Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
13_map_reduce.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19// Example 13 -- A naive and imperative map/reduce, ish
20
21#include <stdint.h>
22#include <stdio.h>
23
24#include <serene/rt/trace.h>
25#include <serene/runtime.h>
26
27#include "serene/utils.h"
28
29#define NUMBER_OF_ELEMENTS 1000
30
31typedef uint64_t (*mapper_f)(uint64_t v);
32
33typedef struct mapper_args_t {
34 uint64_t val;
37
38static uint64_t add_1(uint64_t v) { return v + 10; }
39
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}
47
48static int main_13(srn_context_t *ctx, int argc, char **argv) {
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}
78
uint64_t(* mapper_f)(uint64_t v)
static uint64_t add_1(uint64_t v)
static int main_13(srn_context_t *ctx, int argc, char **argv)
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
void * srn_fiber_result_t
What a fiber's entry produces, type-erased.
Definition fiber.h:161
#define SERENE_MAIN(f)
Definition runtime.h:65
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
Platform neutral static tracepoints.
#define UNUSED(x)
Definition utils.h:45