| 15 |
#define YSIZE 500 |
#define YSIZE 500 |
| 16 |
#include "algorithm.h" |
#include "algorithm.h" |
| 17 |
|
|
| 18 |
|
BOOL VERBOSE = FALSE; |
| 19 |
|
|
| 20 |
|
|
| 21 |
/* BMP Header */ |
/* BMP Header */ |
| 22 |
unsigned char header[54]={0x42,0x4d, // signature BM |
unsigned char header[54]={0x42,0x4d, // signature BM |
| 119 |
|
|
| 120 |
// get worker arguments |
// get worker arguments |
| 121 |
args = (PWORKERARGS)lpParam; |
args = (PWORKERARGS)lpParam; |
|
|
|
|
printf("----------------------------------------------\n"); |
|
|
printf("thread_id: %i\n", thread_id); |
|
|
printf("arg.start_row: %i\n", args->start_row); |
|
|
printf("arg.number_of_rows: %i\n", args->number_of_rows); |
|
| 122 |
|
|
| 123 |
// calculate pointer to beginning of segment |
// calculate pointer to beginning of segment |
| 124 |
pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE); |
pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE); |
| 125 |
printf("segment_start: %p\n", pDataBitmapSegment); |
|
| 126 |
|
// debugging |
| 127 |
|
if (VERBOSE) { |
| 128 |
|
printf("----------------------------------------------\n"); |
| 129 |
|
printf("thread_id: %i\n", thread_id); |
| 130 |
|
printf("arg.start_row: %i\n", args->start_row); |
| 131 |
|
printf("arg.number_of_rows: %i\n", args->number_of_rows); |
| 132 |
|
printf("segment_start: %p\n", pDataBitmapSegment); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
// calculate fractal |
// calculate fractal |
| 136 |
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--) { |
| 138 |
for (x = 0; x < XSIZE; x++) { |
for (x = 0; x < XSIZE; x++) { |
| 139 |
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]); |
| 140 |
|
|
|
// debugging |
|
|
//printf("pointer: %p\n", pDataBitmapCurrent); |
|
|
|
|
| 141 |
// transfer color values to current pixel |
// transfer color values to current pixel |
| 142 |
pDataBitmapSegment[0] = bgr[0]; |
pDataBitmapSegment[0] = bgr[0]; |
| 143 |
pDataBitmapSegment[1] = bgr[1]; |
pDataBitmapSegment[1] = bgr[1]; |
| 147 |
pDataBitmapSegment += 3; |
pDataBitmapSegment += 3; |
| 148 |
|
|
| 149 |
} |
} |
| 150 |
//no padding required because 1500%4 =0 |
//no padding required because 1500%4 =0 ??? |
| 151 |
} |
} |
| 152 |
|
|
| 153 |
printf("thread finished: %i\n", thread_id); |
if (VERBOSE) |
| 154 |
|
printf("thread finished: %i\n", thread_id); |
| 155 |
return 0; |
return 0; |
| 156 |
|
|
| 157 |
} |
} |
| 163 |
DWORD err; |
DWORD err; |
| 164 |
HANDLE hMap, hFile; |
HANDLE hMap, hFile; |
| 165 |
LPVOID pData; |
LPVOID pData; |
| 166 |
unsigned char *pDataBitmap, *pDataBitmapCurrent; |
unsigned char *pDataBitmap; |
| 167 |
|
|
| 168 |
// workers |
// workers |
| 169 |
int workers = 10; |
int workers = 50; |
| 170 |
int worker_index, worker_rows, worker_startrow; |
int worker_index, worker_rows, worker_startrow; |
| 171 |
HANDLE *worker_handles; |
HANDLE *worker_handles; |
|
//struct WorkerArguments *worker_args; |
|
| 172 |
PWORKERARGS worker_args; |
PWORKERARGS worker_args; |
| 173 |
|
|
| 174 |
|
|
| 199 |
pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); |
pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); |
| 200 |
|
|
| 201 |
// debugging |
// debugging |
| 202 |
printf("pos. of file: %p\n", pData); |
if (VERBOSE) { |
| 203 |
printf("pos. of bitmap: %p\n", pDataBitmap); |
printf("pos. of file: %p\n", pData); |
| 204 |
|
printf("pos. of bitmap: %p\n", pDataBitmap); |
| 205 |
// pointer to current pixel |
} |
|
pDataBitmapCurrent = pDataBitmap; |
|
| 206 |
|
|
| 207 |
/* |
/* |
| 208 |
// turn bitmap into white wand |
// turn bitmap into white canvas |
| 209 |
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
for (offset = 0; offset < 500 * 500 * 3; offset++) { |
| 210 |
//pDataBitmap[offset] = 255; |
*pDataBitmap = 255; |
| 211 |
*pDataBitmapCurrent = 255; |
pDataBitmap++; |
|
pDataBitmapCurrent++; |
|
| 212 |
} |
} |
| 213 |
|
exit(0); |
| 214 |
*/ |
*/ |
| 215 |
|
|
| 216 |
|
// allocate memory for bitmap |
| 217 |
|
/* |
| 218 |
|
if ((pDataBitmap = malloc(XSIZE * YSIZE * 3 * sizeof(pDataBitmap[0]))) == NULL) |
| 219 |
|
perror("malloc"), exit(1); |
| 220 |
|
*/ |
| 221 |
|
|
| 222 |
// allocate memory for table of all worker handles |
// allocate memory for table of all worker handles |
| 223 |
if ((worker_handles = malloc(workers * sizeof worker_handles[0])) == NULL) |
if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) |
| 224 |
perror("malloc"), exit(1); |
perror("malloc"), exit(1); |
| 225 |
|
|
|
/* |
|
| 226 |
// allocate memory for table of all worker arguments |
// allocate memory for table of all worker arguments |
| 227 |
if ((worker_args = malloc(workers * sizeof worker_args[0])) == NULL) |
if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) |
| 228 |
perror("malloc"), exit(1); |
perror("malloc"), exit(1); |
| 229 |
*/ |
|
|
// allocate memory for table of all worker arguments |
|
|
worker_args = (PWORKERARGS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, workers * sizeof(WORKERARGS)); |
|
| 230 |
|
|
| 231 |
// calculate segments of bitmap for worker threads/processes and start them |
// calculate segments of bitmap for worker threads/processes and start them |
| 232 |
worker_rows = YSIZE / workers; |
worker_rows = YSIZE / workers; |
| 233 |
printf("rows for each worker: %i\n", worker_rows); |
if (VERBOSE) |
| 234 |
|
printf("rows for each worker: %i\n", worker_rows); |
| 235 |
for (worker_index = 0; worker_index < workers; worker_index++) { |
for (worker_index = 0; worker_index < workers; worker_index++) { |
| 236 |
|
|
| 237 |
// debugging: just run with single thread |
// debugging: just run single thread |
| 238 |
//if (worker_index == 1) |
//if (worker_index == 1) |
| 239 |
// continue; |
// continue; |
| 240 |
|
|
| 244 |
// recalculate number of rows for last worker if (YSIZE mod workers) != 0 |
// recalculate number of rows for last worker if (YSIZE mod workers) != 0 |
| 245 |
if (worker_index == workers - 1) { |
if (worker_index == workers - 1) { |
| 246 |
worker_rows = YSIZE - worker_startrow; |
worker_rows = YSIZE - worker_startrow; |
| 247 |
printf("rows for last worker: %i\n", worker_rows); |
if (VERBOSE) |
| 248 |
|
printf("rows for last worker: %i\n", worker_rows); |
| 249 |
} |
} |
| 250 |
|
|
| 251 |
worker_args[worker_index].start_row = worker_startrow; |
worker_args[worker_index].start_row = worker_startrow; |
| 255 |
worker_handles[worker_index] = CreateThread( |
worker_handles[worker_index] = CreateThread( |
| 256 |
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, |
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, |
| 257 |
0, // SIZE_T dwStackSize, |
0, // SIZE_T dwStackSize, |
| 258 |
fractal_thread, // LPTHREAD_START_ROUTINE lpStartAddress, |
&fractal_thread, // LPTHREAD_START_ROUTINE lpStartAddress, |
| 259 |
&worker_args[worker_index], // LPVOID lpParameter, |
&worker_args[worker_index], // LPVOID lpParameter, |
| 260 |
0, // DWORD dwCreationFlags, |
0, // DWORD dwCreationFlags, |
| 261 |
NULL // LPDWORD lpThreadId |
NULL // LPDWORD lpThreadId |
| 264 |
} |
} |
| 265 |
|
|
| 266 |
// wait for all threads |
// wait for all threads |
| 267 |
|
if (VERBOSE) |
| 268 |
|
printf("waiting...\n"); |
| 269 |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) |
| 270 |
perror("WaitForMultipleObjects"); |
perror("WaitForMultipleObjects"); |
| 271 |
|
|
| 272 |
// debugging: just run with single thread |
// debugging: just run single thread |
| 273 |
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
//if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) |
| 274 |
// perror("WaitForSingleObject"); |
// perror("WaitForSingleObject"); |
| 275 |
|
|
| 296 |
printErrorAndExit("Error at CloseHandle", err); |
printErrorAndExit("Error at CloseHandle", err); |
| 297 |
} |
} |
| 298 |
|
|
| 299 |
|
free(worker_args); |
| 300 |
|
free(worker_handles); |
| 301 |
|
|
| 302 |
|
return 0; |
| 303 |
|
|
| 304 |
} |
} |