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