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