| 96 |
|
|
| 97 |
// arguments for each thread |
// arguments for each thread |
| 98 |
typedef struct _WorkerArguments { |
typedef struct _WorkerArguments { |
| 99 |
unsigned int start_row; |
int start_row; |
| 100 |
unsigned int number_of_rows; |
int number_of_rows; |
| 101 |
unsigned char * pBitmap; |
unsigned char * pBitmap; |
| 102 |
} WORKERARGS, *PWORKERARGS; |
} WORKERARGS, *PWORKERARGS; |
| 103 |
|
|
| 107 |
// thread stuff |
// thread stuff |
| 108 |
int thread_id; |
int thread_id; |
| 109 |
PWORKERARGS args; |
PWORKERARGS args; |
| 110 |
|
unsigned char *pDataBitmapSegment; |
| 111 |
|
|
| 112 |
// fractal calculation |
// fractal calculation |
| 113 |
int x, y; |
int x, y; |
| 118 |
// get worker arguments |
// get worker arguments |
| 119 |
args = (PWORKERARGS)lpParam; |
args = (PWORKERARGS)lpParam; |
| 120 |
|
|
| 121 |
|
printf("----------------------------------------------\n"); |
| 122 |
printf("thread_id: %i\n", thread_id); |
printf("thread_id: %i\n", thread_id); |
| 123 |
printf("arg.start_row: %i\n", args->start_row); |
printf("arg.start_row: %i\n", args->start_row); |
| 124 |
printf("arg.number_of_rows: %i\n", args->number_of_rows); |
printf("arg.number_of_rows: %i\n", args->number_of_rows); |
| 125 |
|
|
| 126 |
|
// calculate pointer to beginning of segment |
| 127 |
|
pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE); |
| 128 |
|
printf("segment_start: %p\n", pDataBitmapSegment); |
| 129 |
|
|
| 130 |
// calculate fractal |
// calculate fractal |
| 131 |
for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { |
for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { |
| 132 |
|
//printf("calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); |
| 133 |
for (x = 0; x < XSIZE; x++) { |
for (x = 0; x < XSIZE; x++) { |
| 134 |
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); |
| 135 |
|
|
| 137 |
//printf("pointer: %p\n", pDataBitmapCurrent); |
//printf("pointer: %p\n", pDataBitmapCurrent); |
| 138 |
|
|
| 139 |
// transfer color values to current pixel |
// transfer color values to current pixel |
| 140 |
args->pBitmap[0] = bgr[0]; |
pDataBitmapSegment[0] = bgr[0]; |
| 141 |
args->pBitmap[1] = bgr[1]; |
pDataBitmapSegment[1] = bgr[1]; |
| 142 |
args->pBitmap[2] = bgr[2]; |
pDataBitmapSegment[2] = bgr[2]; |
| 143 |
|
|
| 144 |
// move pointer to next pixel |
// move pointer to next pixel |
| 145 |
args->pBitmap += 3; |
pDataBitmapSegment += 3; |
| 146 |
|
|
| 147 |
} |
} |
| 148 |
//no padding required because 1500%4 =0 |
//no padding required because 1500%4 =0 |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
|
printf("thread finished: %i\n", thread_id); |
| 152 |
return 0; |
return 0; |
| 153 |
|
|
| 154 |
} |
} |
| 163 |
unsigned char *pDataBitmap, *pDataBitmapCurrent; |
unsigned char *pDataBitmap, *pDataBitmapCurrent; |
| 164 |
|
|
| 165 |
// workers |
// workers |
| 166 |
unsigned int workers = 3; |
int workers = 5; |
| 167 |
unsigned int worker_index, worker_rows, worker_startrow; |
int worker_index, worker_rows, worker_startrow; |
| 168 |
HANDLE *worker_handles; |
HANDLE *worker_handles; |
| 169 |
//struct WorkerArguments *worker_args; |
//struct WorkerArguments *worker_args; |
| 170 |
PWORKERARGS worker_args; |
PWORKERARGS worker_args; |
| 217 |
if ((worker_handles = malloc(workers * sizeof worker_handles[0])) == NULL) |
if ((worker_handles = malloc(workers * sizeof worker_handles[0])) == NULL) |
| 218 |
perror("malloc"), exit(1); |
perror("malloc"), exit(1); |
| 219 |
|
|
|
/* |
|
| 220 |
// allocate memory for table of all worker arguments |
// allocate memory for table of all worker arguments |
| 221 |
if ((worker_args = malloc(workers * sizeof worker_args[0])) == NULL) |
if ((worker_args = malloc(workers * sizeof worker_args[0])) == NULL) |
| 222 |
perror("malloc"), exit(1); |
perror("malloc"), exit(1); |
| 223 |
*/ |
|
| 224 |
// allocate memory for table of all worker arguments |
// allocate memory for table of all worker arguments |
| 225 |
worker_args = (PWORKERARGS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, workers * sizeof(WORKERARGS)); |
/* |
| 226 |
|
worker_args = (PWORKERARGS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_NO_SERIALIZE, workers * sizeof(WORKERARGS)); |
| 227 |
|
|
| 228 |
|
if (worker_args == NULL) { |
| 229 |
|
printErrorAndExit("Failed to allocate on heap", GetLastError()); |
| 230 |
|
} |
| 231 |
|
*/ |
| 232 |
|
|
| 233 |
// calculate segments of bitmap for worker threads/processes and start them |
// calculate segments of bitmap for worker threads/processes and start them |
| 234 |
worker_rows = YSIZE / workers; |
worker_rows = YSIZE / workers; |
| 235 |
printf("rows for each worker: %i\n", worker_rows); |
printf("rows for each worker: %i\n", worker_rows); |
| 236 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
for (worker_index = 0; worker_index < workers; worker_index++) { |
| 237 |
|
|
| 238 |
|
// debugging: just run with single thread |
| 239 |
|
//if (worker_index == 1) |
| 240 |
|
// continue; |
| 241 |
|
|
| 242 |
// number of row to start for each worker |
// number of row to start for each worker |
| 243 |
worker_startrow = worker_index * worker_rows; |
worker_startrow = worker_index * worker_rows; |
| 244 |
|
|
| 266 |
// wait for all threads |
// wait for all threads |
| 267 |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
| 268 |
perror("WaitForMultipleObjects"); |
perror("WaitForMultipleObjects"); |
| 269 |
|
|
| 270 |
|
// debugging: just run with single thread |
| 271 |
|
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
| 272 |
|
// perror("WaitForSingleObject"); |
| 273 |
|
|
| 274 |
|
// close all worker handles |
| 275 |
|
for (worker_index = 0; worker_index < workers; worker_index++) |
| 276 |
|
CloseHandle(worker_handles[worker_index]); |
| 277 |
|
|
| 278 |
/* write the result into the file */ |
/* write the result into the file */ |
| 279 |
if (!FlushViewOfFile(pData, 0)) { |
if (!FlushViewOfFile(pData, 0)) { |
| 280 |
err = GetLastError(); |
err = GetLastError(); |
| 294 |
printErrorAndExit("Error at CloseHandle", err); |
printErrorAndExit("Error at CloseHandle", err); |
| 295 |
} |
} |
| 296 |
|
|
| 297 |
|
/* |
| 298 |
|
if (HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY | HEAP_NO_SERIALIZE, worker_args) != TRUE) { |
| 299 |
|
printErrorAndExit("Call to HeapFree has failed", GetLastError()); |
| 300 |
|
} |
| 301 |
|
*/ |
| 302 |
|
|
| 303 |
|
|
| 304 |
|
return 0; |
| 305 |
|
|
| 306 |
} |
} |