Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
13_map_reduce.c File Reference
#include <stdint.h>
#include <stdio.h>
#include <serene/rt/trace.h>
#include <serene/runtime.h>
#include "serene/utils.h"
Include dependency graph for 13_map_reduce.c:

Go to the source code of this file.

Data Structures

struct  mapper_args_t

Macros

#define NUMBER_OF_ELEMENTS   1000

Typedefs

typedef uint64_t(* mapper_f) (uint64_t v)
typedef struct mapper_args_t mapper_args_t

Functions

static uint64_t add_1 (uint64_t v)
static srn_fiber_result_t mapper_fiber (srn_context_t *ctx, void *arg)
static int main_13 (srn_context_t *ctx, int argc, char **argv)

Macro Definition Documentation

◆ NUMBER_OF_ELEMENTS

#define NUMBER_OF_ELEMENTS   1000

Definition at line 29 of file 13_map_reduce.c.

Typedef Documentation

◆ mapper_args_t

typedef struct mapper_args_t mapper_args_t

◆ mapper_f

typedef uint64_t(* mapper_f) (uint64_t v)

Definition at line 31 of file 13_map_reduce.c.

Function Documentation

◆ add_1()

uint64_t add_1 ( uint64_t v)
static

Definition at line 38 of file 13_map_reduce.c.

38{ return v + 10; }
Here is the caller graph for this function:

◆ main_13()

int main_13 ( srn_context_t * ctx,
int argc,
char ** argv )
static

Definition at line 48 of file 13_map_reduce.c.

48 {
49 UNUSED(argc);
50 UNUSED(argv);
51
53
54 uint64_t xs[NUMBER_OF_ELEMENTS] = {0};
55
56 for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
57 // Fan out, one mapper per element. SRN_FIBER_SPAWN_COPY copies the argument
58 // into the context, so each fiber owns its own copy and the loop-local
59 // `args` can die at the end of the iteration.
61 .val = i,
62 .f = &add_1,
63 };
64 fibers[i] = SRN_FIBER_SPAWN_COPY(ctx, mapper_fiber, args);
65 }
66
67 // Fan in, join each mapper and read the value it returned.
68 for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
69 xs[i] = (uint64_t)(uintptr_t)srn_fiber_wait_for(fibers[i]);
70 }
71
72 for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
73 printf(">> %lu\n", xs[i]);
74 };
75
76 return 0;
77}
static uint64_t add_1(uint64_t v)
static srn_fiber_result_t mapper_fiber(srn_context_t *ctx, void *arg)
#define NUMBER_OF_ELEMENTS
va_list args
Definition acutest.h:876
#define SRN_FIBER_SPAWN_COPY(ctx, entry, value)
srn_fiber_spawn_copy for an lvalue, the address and size are taken for the caller.
Definition fiber.h:474
srn_fiber_result_t srn_fiber_wait_for(srn_fiber_t *target)
Block the calling fiber until target finishes, then return its result.
Definition scheduler.c:1130
#define UNUSED(x)
Definition utils.h:45
Here is the call graph for this function:

◆ mapper_fiber()

srn_fiber_result_t mapper_fiber ( srn_context_t * ctx,
void * arg )
static

Definition at line 40 of file 13_map_reduce.c.

40 {
41 (void)ctx;
42 auto mapper_args = (mapper_args_t *)arg;
43
44 uint64_t val = mapper_args->f(mapper_args->val);
45 return (void *)(uintptr_t)val;
46}
Here is the caller graph for this function: