CYLINDER_LIST * guess_cylinder_list (int *working_area_total, NEURON_LAYER *trunk_segmentation_map, NEURON_LAYER *disparity_map) { CYLINDER guessed_cylinder; CYLINDER_LIST *cylinder_list = alloc_cylinder_list (); double *line; int lines_number, l; int line_size; double *alphas; int *ys; int working_area_percent[4]; int *down_transition, *up_transition; double *point_3D_left, *point_3D_right; working_area_percent[0] = (working_area_total[0] + working_area_total[2])/2.0 - WORKING_AREA_PERCENTAGE/2.0 * (working_area_total[2]-working_area_total[0]) + 0.5; working_area_percent[1] = working_area_total[1]; working_area_percent[2] = (working_area_total[0] + working_area_total[2])/2.0 + WORKING_AREA_PERCENTAGE/2.0 * (working_area_total[2]-working_area_total[0]) + 0.5; working_area_percent[3] = working_area_total[3]; line_size = working_area_percent[2] - working_area_percent[0] + 1; line = (double *) malloc (line_size * sizeof (double)); alphas = (double *) malloc ((working_area_percent[3] - working_area_percent[1]) * sizeof (double)); ys = (int *) malloc ((working_area_percent[3] - working_area_percent[1]) * sizeof (int)); down_transition = (int *) malloc ((working_area_percent[3] - working_area_percent[1]) * sizeof (int)); up_transition = (int *) malloc ((working_area_percent[3] - working_area_percent[1]) * sizeof (int)); find_lines (alphas, ys, down_transition, up_transition, &lines_number, line, line_size, trunk_segmentation_map, working_area_percent); convert_2D_to_3D (&point_3D_left, &point_3D_right, alphas, ys, down_transition, up_transition, lines_number, trunk_segmentation_map, disparity_map, working_area_percent, line, line_size); for(l = 0; l < lines_number; l++) { guess_cylinder (&guessed_cylinder, &(point_3D_right[l*12+3]), &(point_3D_right[l*12+6]), &(point_3D_right[l*12+9]), alphas[l]); cylinder_list_add (cylinder_list, guessed_cylinder); } free (point_3D_left); free (point_3D_right); free (down_transition); free (up_transition); free (line); free (alphas); free (ys); return cylinder_list; }
static int lazymanifest_init(lazymanifest *self, PyObject *args) { char *data; Py_ssize_t len; int err, ret; PyObject *pydata; if (!PyArg_ParseTuple(args, "S", &pydata)) { return -1; } err = PyString_AsStringAndSize(pydata, &data, &len); self->dirty = false; if (err == -1) return -1; self->pydata = pydata; Py_INCREF(self->pydata); Py_BEGIN_ALLOW_THREADS self->lines = malloc(DEFAULT_LINES * sizeof(line)); self->maxlines = DEFAULT_LINES; self->numlines = 0; if (!self->lines) ret = MANIFEST_OOM; else ret = find_lines(self, data, len); Py_END_ALLOW_THREADS switch (ret) { case 0: break; case MANIFEST_OOM: PyErr_NoMemory(); break; case MANIFEST_NOT_SORTED: PyErr_Format(PyExc_ValueError, "Manifest lines not in sorted order."); break; case MANIFEST_MALFORMED: PyErr_Format(PyExc_ValueError, "Manifest did not end in a newline."); break; default: PyErr_Format(PyExc_ValueError, "Unknown problem parsing manifest."); } return ret == 0 ? 0 : -1; }
int main(int argc, char *argv[]) { if(argc != 3) { fprintf(stderr, "There's the wrong number of arguments!\n"); fprintf(stderr, "printdists [function to perform] [filename]\n"); exit(1); } FILENAME = argv[2]; ELEMENTS = find_lines(FILENAME); fprintf(stderr,"Let's start...\n"); fprintf(stderr, "File we're going to datamine is: %s\n", FILENAME); fprintf(stderr, "It has this many elements: %d\n", ELEMENTS); if (strcmp(argv[1], "printdists") == 0) { fprintf(stderr, "Calling stackdistlist.\n"); float* dists = stackdistlist(FILENAME, ELEMENTS); fprintf(stderr, "Printing floats.\n"); printfloatlist(dists, ELEMENTS - 1); fprintf(stderr, "Done!\n"); } else if (strcmp(argv[1], "printvols") == 0) { fprintf(stderr, "Calling vol_list.\n"); float* vols = vol_list(FILENAME, ELEMENTS); fprintf(stderr, "Printing floats.\n"); printfloatlist(vols, ELEMENTS / 4); fprintf(stderr, "Done!\n"); } else if (strcmp(argv[1], "printangles") == 0) { fprintf(stderr, "Calling angle_list.\n"); angle_list(FILENAME, ELEMENTS); fprintf(stderr, "Done!\n"); } else { fprintf(stderr, "Invalid function.\n"); } return 0; }
int main(int argc, char *argv[]) { // CLI argument parsing if (argc != 4) { fprintf(stderr, "Expected 3 arguments, got %d\nExiting...\n", argc - 1); exit(1); } else { char *end; uint32_t thread_count = (uint32_t) strtol(argv[1], &end, 10); if (*end) { fprintf(stderr, "Error, expects first argument to be a power of two," " was '%s'.\n", argv[1]); exit(1); } if (!(thread_count && !(thread_count & (thread_count - 1)))) { fprintf(stderr, "Error, first argument must be a power of two, not %d.\n", thread_count); exit(1); } fprintf(stderr, "Looking for '%s' in file %s using %d threads...\n", argv[2], argv[3], thread_count); char *search_term = argv[2]; FILE *fp; long file_size; char *buffer; fp = fopen(argv[3], "rb"); if (!fp) { perror(argv[1]); exit(1); } fseek(fp, 0, SEEK_END); file_size = ftell(fp); rewind(fp); buffer = malloc(file_size + 1); if (!buffer) { fclose(fp); fprintf(stderr, "Cannot allocate buffer for file (probably too large).\n"); exit(1); } buffer[file_size] = 0; if (1 != fread(buffer, file_size, 1, fp)) { fclose(fp); free(buffer); fprintf(stderr, "Entire read failed.\n"); exit(1); } else { fclose(fp); } // Find all lines pthread_t *tid = malloc(thread_count * sizeof(pthread_t)); LineSplitArgs *lsa = malloc(thread_count * sizeof(LineSplitArgs)); uint32_t *intervals = malloc(thread_count * sizeof(uint32_t)); struct timespec time_start, time_stop; long long nanoseconds_elapsed; for (uint32_t i = 0; i < thread_count; i++) { LineSplitArgs arguments = { .buffer = buffer, .file_size = (uint32_t) file_size, .own_tid = i, .thread_count = thread_count, .intervals = intervals }; lsa[i] = arguments; } clock_gettime(CLOCK_MONOTONIC, &time_start); for (uint32_t i = 0; i < thread_count; i++) { pthread_create(&tid[i], NULL, &split_to_lines, (void *) &lsa[i]); } for (uint32_t i = 0; i < thread_count; i++) { pthread_join(tid[i], NULL); } clock_gettime(CLOCK_MONOTONIC, &time_stop); nanoseconds_elapsed = (time_stop.tv_sec - time_start.tv_sec) * 1000000000LL; nanoseconds_elapsed += (time_stop.tv_nsec - time_start.tv_nsec); long long line_split = nanoseconds_elapsed; free(lsa); // Concatenate work ConcatArgs *ca = malloc(thread_count * sizeof(ConcatArgs)); pthread_barrier_t *barriers = malloc(thread_count * sizeof(pthread_barrier_t)); for (uint32_t i = 0; i < thread_count; i++) { pthread_barrier_init(&barriers[i], NULL, 2); ConcatArgs args = {.own_tid = i, .thread_count = thread_count, .intervals = intervals, .barriers = barriers }; ca[i] = args; } clock_gettime(CLOCK_MONOTONIC, &time_start); for (uint32_t i = 0; i < thread_count; i++) { pthread_create(&tid[i], NULL, &concatenate_arrays, (void *) &ca[i]); } for (uint32_t i = 0; i < thread_count; i++) { pthread_join(tid[i], NULL); } clock_gettime(CLOCK_MONOTONIC, &time_stop); nanoseconds_elapsed = (time_stop.tv_sec - time_start.tv_sec) * 1000000000LL; nanoseconds_elapsed += (time_stop.tv_nsec - time_start.tv_nsec); long long line_concat = nanoseconds_elapsed; uint32_t line_count = intervals[0]; // Actually find lines (done here, to avoid cache modification) IntervalArray *lines = find_lines(buffer, (uint32_t) file_size, thread_count); // Found all lines, now onto searching in each list fprintf(stderr, "%d newlines\n", line_count); FilterArgs *fa = malloc(thread_count * sizeof(FilterArgs)); // Reuse intervals array for (uint32_t i = 0; i < thread_count; i++) { FilterArgs arguments = { .buffer = buffer, .search_term = search_term, .lines = lines, .intervals = intervals, .own_tid = i, .thread_count = thread_count }; fa[i] = arguments; } clock_gettime(CLOCK_MONOTONIC, &time_start); for (uint32_t i = 0; i < thread_count; i++) { pthread_create(&tid[i], NULL, &filter_by_term, (void *) &fa[i]); } for (uint32_t i = 0; i < thread_count; i++) { pthread_join(tid[i], NULL); } clock_gettime(CLOCK_MONOTONIC, &time_stop); nanoseconds_elapsed = (time_stop.tv_sec - time_start.tv_sec) * 1000000000LL; nanoseconds_elapsed += (time_stop.tv_nsec - time_start.tv_nsec); long long search_lines = nanoseconds_elapsed; // Concatenate work // Reuse concat args and barriers for (uint32_t i = 0; i < thread_count; i++) { pthread_barrier_init(&barriers[i], NULL, 2); ConcatArgs args = {.own_tid = i, .thread_count = thread_count, .intervals = intervals, .barriers = barriers }; ca[i] = args; } clock_gettime(CLOCK_MONOTONIC, &time_start); for (uint32_t i = 0; i < thread_count; i++) { pthread_create(&tid[i], NULL, &concatenate_arrays, (void *) &ca[i]); } for (uint32_t i = 0; i < thread_count; i++) { pthread_join(tid[i], NULL); } clock_gettime(CLOCK_MONOTONIC, &time_stop); nanoseconds_elapsed = (time_stop.tv_sec - time_start.tv_sec) * 1000000000LL; nanoseconds_elapsed += (time_stop.tv_nsec - time_start.tv_nsec); long long search_concat = nanoseconds_elapsed; fprintf(stderr, "%d hits\n", intervals[0]); printf("%lld %lld %lld %lld %lld\n", line_split, line_concat, search_lines, search_concat, line_split + line_concat + search_lines + search_concat); interval_array_destroy(lines); free(intervals); free(buffer); free(ca); free(barriers); exit(0); } } // Unmeasured stuff, to actually FIND the lines we're parallelizing over static IntervalArray* find_lines(char* buffer, uint32_t file_size, uint32_t thread_count) { pthread_t *tid = malloc(thread_count * sizeof(pthread_t)); LineSplitArgsUnmeasured *lsa = malloc(thread_count * sizeof(LineSplitArgsUnmeasured)); IntervalArray **intervals = malloc(thread_count * sizeof(IntervalArray *)); for (uint32_t i = 0; i < thread_count; i++) { LineSplitArgsUnmeasured arguments = { .buffer = buffer, .file_size = (uint32_t) file_size, .own_tid = i, .thread_count = thread_count, .intervals = intervals }; lsa[i] = arguments; } for (uint32_t i = 0; i < thread_count; i++) { pthread_create(&tid[i], NULL, &split_to_lines_unmeasured, (void *) &lsa[i]); } for (uint32_t i = 0; i < thread_count; i++) { pthread_join(tid[i], NULL); } free(lsa); // Concatenate work ConcatArgsUnmeasured *ca = malloc(thread_count * sizeof(ConcatArgsUnmeasured)); pthread_barrier_t *barriers = malloc(thread_count * sizeof(pthread_barrier_t)); for (uint32_t i = 0; i < thread_count; i++) { pthread_barrier_init(&barriers[i], NULL, 2); ConcatArgsUnmeasured args = {.own_tid = i, .thread_count = thread_count, .intervals = intervals, .barriers = barriers }; ca[i] = args; } for (uint32_t i = 0; i < thread_count; i++) { pthread_create(&tid[i], NULL, &concatenate_arrays_unmeasured, (void *) &ca[i]); } for (uint32_t i = 0; i < thread_count; i++) { pthread_join(tid[i], NULL); } IntervalArray *lines = intervals[0]; free(tid); free(barriers); free(ca); free(intervals); return lines; } static void* split_to_lines_unmeasured(void *void_input) { LineSplitArgsUnmeasured *lsa = (LineSplitArgsUnmeasured *) void_input; const char *buffer = lsa->buffer; const uint32_t own_tid = lsa->own_tid; const uint32_t file_size = lsa->file_size; const uint32_t thread_count = lsa->thread_count; IntervalArray **intervals = lsa->intervals; // calculate the interval to compute for uint32_t partition_size = file_size / thread_count; uint32_t from = partition_size * own_tid; uint32_t to = partition_size * (own_tid + 1); if (own_tid + 1 == thread_count) { to = file_size; } // find the lines IntervalArray *lines = interval_array_create(); // rewind to start of line uint32_t line_start = from; while (0 < line_start && lsa->buffer[line_start] != '\n') { line_start--; } // find and collect lines for (uint32_t i = from; i < to; i++) { if (buffer[i] == '\n') { Interval interval = {.from = line_start, .to = i}; interval_array_add(lines, interval); line_start = i + 1; } }