source: proiecte/swift/trunk/test/fibo_2spawn.c @ 176

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 4.8 KB
Line 
1/*
2 * swift_context.c
3 *
4 * (c) 2009 Ionut Rosoiu <ionut.rosoiu@gmail.com>
5 *
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include "swift.h"
11
12typedef struct fibo_data {
13        int n;
14        int *r;
15        // sync related
16        swift_size_t sync_frames_remaining;
17        char _pad[SWIFT_CACHE_LINE_SIZE - sizeof(swift_size_t)];
18} fibo_data_t;
19
20/* forward declaration for the fibo() */
21void fibo(swift_thread_t *thread, swift_frame_t *frame);
22
23
24/* void fibo(int r, shared_w int* r) */
25void fibo(swift_thread_t *thread, swift_frame_t *frame) {
26        fibo_data_t *data = frame->private_data;
27        swift_status_t status;
28        swift_size_t n;
29
30        SWIFT_LOG_FRAME_INFO_STR("\nfibo() ", thread, frame);
31
32        if (data->n < 2) {
33                *data->r = data->n;
34                data->sync_frames_remaining = 0;
35
36        } else {
37                // fibo(n-1) data
38                swift_frame_t *frame1;
39                fibo_data_t *data1;
40
41                // fibo(n-2) data
42                swift_frame_t *frame2;
43                fibo_data_t *data2;
44
45                int r1, r2;
46
47                /* spawn fibo(n-1, r1); */
48                frame1 = (swift_frame_t*) swift_malloc(thread, sizeof(swift_frame_t) + sizeof(fibo_data_t), &status);
49                frame1->closure = fibo;
50#ifdef LOGGING_ON
51                frame1->creator_id = thread->id;
52                frame1->dbg = data->n - 1;
53#endif
54
55                data1 = (fibo_data_t*) ((char*)frame1 + sizeof(swift_frame_t));
56                data1->n = data->n - 1;
57                data1->r = &r1;
58                frame1->private_data = data1;
59
60                SWIFT_WRITE_FRAME_INFO(frame1, thread->id, 'f', thread->frame_no++);
61                // sync related
62                frame1->dependencies_frame = NULL;
63                frame1->sync_frames_remaining = &data->sync_frames_remaining;
64
65                /* spawn fibo(n-2, r2); */
66                frame2 = (swift_frame_t*) swift_malloc(thread, sizeof(swift_frame_t) + sizeof(fibo_data_t), &status);
67                frame2->closure = fibo;
68#ifdef LOGGING_ON
69                frame2->creator_id = thread->id;
70                frame2->dbg = data->n - 2;
71#endif
72
73                data2 = (fibo_data_t*) ((char*)frame2 + sizeof(swift_frame_t));
74                data2->n = data->n - 2;
75                data2->r = &r2;
76                frame2->private_data = data2;
77
78                SWIFT_WRITE_FRAME_INFO(frame2, thread->id, 'f', thread->frame_no++);
79                frame2->dependencies_frame = NULL;
80                frame2->sync_frames_remaining = &data->sync_frames_remaining;
81
82                // wait for 1,2 to finish
83                data->sync_frames_remaining = 2;
84
85                // add 1 & 2 in the workque
86                swift_deque_push(&thread->workque, frame1, &status);
87                swift_deque_push(&thread->workque, frame2, &status);
88
89                /* ksync; */
90                while ((n = SWIFT_ATOMIC_READ(data->sync_frames_remaining))) {
91                        SWIFT_LOG(INFO, "[%d] %s rem=%d c=%d f=%c.%d (%d)\n", thread->id, "@sync", n,
92                                                SWIFT_PROC(frame->info), SWIFT_FRAME_NAME(frame->info),
93                                                        frame->dbg, SWIFT_FRAME_ID(frame->info));
94                        swift_scheduler_execute(thread, &status);
95                }
96
97                *data->r = r1 + r2;
98        }
99
100        SWIFT_LOG(INFO, "[%d] __fibo(%d)=%d\n", thread->id, data->n, *data->r);
101
102        swift_signal_frame_done(thread, frame, &status);
103        swift_frame_done(thread->context, thread, frame, &status);
104}
105
106void* thread_start(void *arg) {
107        swift_thread_t *thread = arg;
108        swift_status_t status;
109
110        SWIFT_LOG(INFO, "_____started thread %d\n", thread->id);
111
112        while (! thread->context->parallel_finished) {
113                swift_scheduler_execute(thread, &status);
114        }
115
116        SWIFT_LOG(INFO, "_____finished thread %d: %d frames processed\n", thread->id, thread->stats);
117        printf("_____finished thread %d: %d frames processed\n", thread->id, thread->stats);
118
119        return arg;
120}
121
122int main(int argc, char **argv) {
123        swift_context_t context;
124        swift_status_t status;
125        swift_frame_t *frame;
126        fibo_data_t *data;
127        int i, r;
128        int nthreads, n;
129
130        if (argc != 3) {
131                fprintf(stderr, "usage: %s Nthreads N\n", argv[0]);
132                abort();
133        }
134
135        nthreads = strtol(argv[1], NULL, 10);
136        n = strtol(argv[2], NULL, 10);
137
138        SWIFT_LOG(INFO, "Started nthreads=%d n=%d\n", nthreads, n);
139
140        /* initialize the context */
141        swift_context_init(&context, nthreads, &status);
142
143        /* push the first frame */
144        frame = (swift_frame_t*) swift_malloc(&context.threads[0], sizeof(swift_frame_t) + sizeof(fibo_data_t), &status);
145        SWIFT_FRAME_SET_END_PARALLEL(frame);
146        frame->creator_id = 0;
147        frame->closure = fibo;
148        frame->dbg = n;
149
150        data = (fibo_data_t*) ((char*)frame + sizeof(swift_frame_t));
151        data->n = n;
152        data->r = &r;
153        frame->private_data = data;
154
155        SWIFT_WRITE_FRAME_INFO(frame, 0, 'f', context.threads[0].frame_no++);
156        SWIFT_LOG_FRAME_INFO((&context.threads[0]), frame);
157
158        // no wait frame
159        frame->dependencies_no = 0;
160        frame->dependencies_frame = NULL;
161        frame->sync_frames_remaining = &data->sync_frames_remaining; // doesn't matter
162
163        swift_deque_push(&context.threads[0].workque, frame, &status);
164        SWIFT_LOG(INFO, "pushed first frame\n");
165
166
167        /* start the threads */
168        for (i=0; i<nthreads; i++) {
169                swift_thread_start(&context.threads[i], thread_start, &status);
170        }
171
172        /* wait for the threads to finish */
173        for (i=0; i<nthreads; i++) {
174                SWIFT_LOG(INFO, "_____waiting %d\n", i);
175                swift_thread_wait(&context.threads[i], &status);
176                SWIFT_LOG(INFO, "_____waiting %d done\n", i);
177        }
178
179        /* check the result */
180        printf("computed result for fibo(%d)=%d\n", n, r);
181        swift_context_destroy(&context, &status);
182
183        return 0;
184}
Note: See TracBrowser for help on using the repository browser.