source: proiecte/swift/trunk/test/fibo_2spawns_no_ksync.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.5 KB
RevLine 
[176]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 = (swift_frame_t*) swift_malloc(thread, sizeof(swift_frame_t) + sizeof(fibo_data_t), &status);
51                frame1->closure = fibo;
52#ifdef LOGGING_ON
53                frame1->creator_id = thread->id;
54                frame1->dbg = data->n - 1;
55#endif
56
57                data1 = (fibo_data_t*) ((char*)frame1 + sizeof(swift_frame_t));
58                data1->n = data->n - 1;
59                data1->r = &r1;
60                frame1->private_data = data1;
61
62                SWIFT_WRITE_FRAME_INFO(frame1, thread->id, 'f', thread->frame_no++);
63                // sync related
64                frame1->dependencies_frame = NULL;
65                frame1->sync_frames_remaining = &data->sync_frames_remaining;
66
67                /* spawn fibo(n-2, r2); */
68                frame2 = (swift_frame_t*) swift_malloc(thread, sizeof(swift_frame_t) + sizeof(fibo_data_t), &status);
69                frame2->closure = fibo;
70#ifdef LOGGING_ON
71                frame2->creator_id = thread->id;
72                frame2->dbg = data->n - 2;
73#endif
74
75                data2 = (fibo_data_t*) ((char*)frame2 + sizeof(swift_frame_t));
76                data2->n = data->n - 2;
77                data2->r = &r2;
78                frame2->private_data = data2;
79
80                SWIFT_WRITE_FRAME_INFO(frame2, thread->id, 'f', thread->frame_no++);
81                frame2->dependencies_frame = NULL;
82                frame2->sync_frames_remaining = &data->sync_frames_remaining;
83
84                // wait for 1,2 to finish
85                data->sync_frames_remaining = 2;
86
87                // add 1 & 2 in the workque
88                swift_deque_push(&thread->workque, frame1, &status);
89                swift_deque_push(&thread->workque, frame2, &status);
90
91                *data->r = r1 + r2;
92        }
93
94        SWIFT_LOG(INFO, "[%d] __fibo(%d)=%d\n", thread->id, data->n, *data->r);
95
96        swift_signal_frame_done(thread, frame, &status);
97        swift_frame_done(thread->context, thread, frame, &status);
98}
99
100void* thread_start(void *arg) {
101        swift_thread_t *thread = arg;
102        swift_status_t status;
103        int i = 0;
104
105        SWIFT_LOG(INFO, "_____started thread %d\n", thread->id);
106
107        while (i++ <= N) {
108                swift_scheduler_execute(thread, &status);
109        }
110
111        SWIFT_LOG(INFO, "_____finished thread %d: %d frames processed\n", thread->id, thread->stats);
112        printf("_____finished thread %d: %d frames processed\n", thread->id, thread->stats);
113
114        return arg;
115}
116
117int main(int argc, char **argv) {
118        swift_context_t context;
119        swift_status_t status;
120        swift_frame_t *frame;
121        fibo_data_t *data;
122        int i, r;
123        int nthreads, n;
124
125        if (argc != 3) {
126                fprintf(stderr, "usage: %s Nthreads N\n", argv[0]);
127                abort();
128        }
129
130        nthreads = strtol(argv[1], NULL, 10);
131        n = strtol(argv[2], NULL, 10);
132        N = n;
133
134        SWIFT_LOG(INFO, "Started nthreads=%d n=%d\n", nthreads, n);
135
136        /* initialize the context */
137        swift_context_init(&context, nthreads, &status);
138
139        /* push the first frame */
140        frame = (swift_frame_t*) swift_malloc(&context.threads[0], sizeof(swift_frame_t) + sizeof(fibo_data_t), &status);
141        SWIFT_FRAME_SET_END_PARALLEL(frame);
142        frame->creator_id = 0;
143        frame->closure = fibo;
144        frame->dbg = n;
145
146        data = (fibo_data_t*) ((char*)frame + sizeof(swift_frame_t));
147        data->n = n;
148        data->r = &r;
149        frame->private_data = data;
150
151        SWIFT_WRITE_FRAME_INFO(frame, 0, 'f', context.threads[0].frame_no++);
152        SWIFT_LOG_FRAME_INFO((&context.threads[0]), frame);
153
154        // no wait frame
155        frame->dependencies_no = 0;
156        frame->dependencies_frame = NULL;
157        frame->sync_frames_remaining = &data->sync_frames_remaining; // doesn't matter
158
159        swift_deque_push(&context.threads[0].workque, frame, &status);
160        SWIFT_LOG(INFO, "pushed first frame\n");
161
162
163        /* start the threads */
164        for (i=0; i<nthreads; i++) {
165                swift_thread_start(&context.threads[i], thread_start, &status);
166        }
167
168        /* wait for the threads to finish */
169        for (i=0; i<nthreads; i++) {
170                SWIFT_LOG(INFO, "_____waiting %d\n", i);
171                swift_thread_wait(&context.threads[i], &status);
172                SWIFT_LOG(INFO, "_____waiting %d done\n", i);
173        }
174
175        /* check the result */
176        printf("computed result for fibo(%d)=%d\n", n, r);
177        swift_context_destroy(&context, &status);
178
179        return 0;
180}
Note: See TracBrowser for help on using the repository browser.