source: proiecte/swift/trunk/test/fibo_stack_2spawns.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
12int N;
13
14typedef struct fibo_data {
15        int n;
16        int *r;
17        // sync related
18        swift_size_t sync_frames_remaining;
19        char _pad[SWIFT_CACHE_LINE_SIZE - sizeof(swift_size_t)];
20} fibo_data_t;
21
22/* forward declaration for the fibo() */
23void fibo(swift_thread_t *thread, swift_frame_t *frame);
24
25
26/* void fibo(int r, shared_w int* r) */
27void fibo(swift_thread_t *thread, swift_frame_t *frame) {
28        fibo_data_t *data = frame->private_data;
29        swift_status_t status;
30        swift_size_t n;
31
32        SWIFT_LOG_FRAME_INFO_STR("\nfibo() ", thread, frame);
33
34        if (data->n < 2) {
35                *data->r = data->n;
36                data->sync_frames_remaining = 0;
37
38        } else {
39                // fibo(n-1) data
40                swift_frame_t frame1;
41                fibo_data_t data1;
42
43                // fibo(n-2) data
44                swift_frame_t frame2;
45                fibo_data_t data2;
46
47                int r1, r2;
48
49                /* spawn fibo(n-1, r1); */
50                frame1.closure = fibo;
51                frame1.flags = 0;
52#ifdef LOGGING_ON
53                frame1.creator_id = thread->id;
54                frame1.dbg = data->n - 1;
55#endif
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.closure = fibo;
67                frame2.flags = 0;
68#ifdef LOGGING_ON
69                frame2.creator_id = thread->id;
70                frame2.dbg = data->n - 2;
71#endif
72
73                data2.n = data->n - 2;
74                data2.r = &r2;
75                frame2.private_data = &data2;
76
77                SWIFT_WRITE_FRAME_INFO((&frame2), thread->id, 'f', thread->frame_no++);
78                frame2.dependencies_frame = NULL;
79                frame2.sync_frames_remaining = &data->sync_frames_remaining;
80
81                // wait for 1,2 to finish
82                data->sync_frames_remaining = 2;
83
84                // add 1 & 2 in the workque
85                swift_deque_push(&thread->workque, &frame1, &status);
86                swift_deque_push(&thread->workque, &frame2, &status);
87
88                while ((n = SWIFT_ATOMIC_READ(data->sync_frames_remaining))) {
89                        SWIFT_LOG(INFO, "[%d] %s rem=%d c=%d f=%c.%d (%d)\n", thread->id, "@sync", n,
90                                                                          SWIFT_PROC(frame->info), SWIFT_FRAME_NAME(frame->info),
91                                                                          frame->dbg, SWIFT_FRAME_ID(frame->info));
92                        swift_scheduler_execute(thread);
93                }
94
95                *data->r = r1 + r2;
96        }
97
98        SWIFT_LOG(INFO, "[%d] __fibo(%d)=%d\n", thread->id, data->n, *data->r);
99
100        if (SWIFT_FRAME_IS_END_PARALLEL(frame)) {
101                SWIFT_LOG(INFO, "[%d] END_PARALLEL!!!\n", thread->id);
102                thread->stop = 1;
103        }
104
105        swift_signal_frame_done(thread, frame);
106        swift_frame_done(thread->context, thread, frame);
107}
108
109void* thread_start(void *arg) {
110        swift_thread_t *thread = arg;
111        int i = 0;
112
113        SWIFT_LOG(INFO, "_____started thread %d\n", thread->id);
114
115        //while (i++ <= N) {
116                //swift_scheduler_execute(thread, &status);
117        //}
118
119        while(!thread->stop) {
120                swift_scheduler_execute(thread);
121        }
122
123        // inform all other threads that they must stop
124        for (i=0; i<thread->context->thread_num; i++) {
125                thread->context->threads[i].stop = 1;
126        }
127
128
129        SWIFT_LOG(INFO, "_____finished thread %d: %d frames processed\n", thread->id, thread->stats);
130        printf("_____finished thread %d: %d frames processed\n", thread->id, thread->stats);
131
132        return arg;
133}
134
135int main(int argc, char **argv) {
136        swift_context_t context;
137        swift_frame_t *frame;
138        swift_status_t status;
139        fibo_data_t *data;
140        int i, r;
141        int nthreads, n;
142
143        if (argc != 3) {
144                fprintf(stderr, "usage: %s Nthreads N\n", argv[0]);
145                abort();
146        }
147
148        nthreads = strtol(argv[1], NULL, 10);
149        n = strtol(argv[2], NULL, 10);
150        N = n;
151
152        SWIFT_LOG(INFO, "Started nthreads=%d n=%d\n", nthreads, n);
153
154        /* initialize the context */
155        swift_context_init(&context, nthreads);
156
157        /* push the first frame */
158        frame = (swift_frame_t*) swift_malloc(&context.threads[0], sizeof(swift_frame_t) + sizeof(fibo_data_t));
159        SWIFT_FRAME_SET_END_PARALLEL(frame);
160        frame->closure = fibo;
161#ifdef LOGGING_ON
162        frame->dbg = n;
163        frame->creator_id = 0;
164#endif
165
166        data = (fibo_data_t*) ((char*)frame + sizeof(swift_frame_t));
167        data->n = n;
168        data->r = &r;
169        frame->private_data = data;
170
171        SWIFT_WRITE_FRAME_INFO(frame, 0, 'f', context.threads[0].frame_no++);
172        SWIFT_LOG_FRAME_INFO((&context.threads[0]), frame);
173
174        // no wait frame
175        frame->dependencies_no = 0;
176        frame->dependencies_frame = NULL;
177        frame->sync_frames_remaining = &data->sync_frames_remaining; // doesn't matter
178
179        swift_deque_push(&context.threads[0].workque, frame, &status);
180        SWIFT_LOG(INFO, "pushed first frame\n");
181
182
183        /* start the threads */
184        for (i=0; i<nthreads; i++) {
185                swift_thread_start(&context.threads[i], thread_start);
186        }
187
188        /* wait for the threads to finish */
189        for (i=0; i<nthreads; i++) {
190                SWIFT_LOG(INFO, "_____waiting %d\n", i);
191                swift_thread_wait(&context.threads[i]);
192                SWIFT_LOG(INFO, "_____waiting %d done\n", i);
193        }
194
195        /* check the result */
196        printf("computed result for fibo(%d)=%d\n", n, r);
197        swift_context_destroy(&context);
198
199        return 0;
200}
Note: See TracBrowser for help on using the repository browser.