15 |
#define YSIZE 500 |
#define YSIZE 500 |
16 |
#include "algorithm.h" |
#include "algorithm.h" |
17 |
|
|
18 |
|
#define MAX_COMMAND_LINE 65536 |
19 |
|
#define MMAP_NAME "bmp_fractal" |
20 |
|
|
21 |
BOOL VERBOSE = FALSE; |
BOOL VERBOSE = FALSE; |
22 |
|
|
23 |
|
|
95 |
} |
} |
96 |
|
|
97 |
// close file handle |
// close file handle |
98 |
fclose(fd); |
if (fclose(fd) != 0) { |
99 |
|
perror("Error while closing file handle"); |
100 |
|
exit(1); |
101 |
|
} |
102 |
} |
} |
103 |
|
|
104 |
|
|
121 |
int x, y; |
int x, y; |
122 |
char bgr[3]; |
char bgr[3]; |
123 |
|
|
|
thread_id = GetCurrentThreadId(); |
|
|
|
|
124 |
// get worker arguments |
// get worker arguments |
125 |
args = (PWORKERARGS)lpParam; |
args = (PWORKERARGS)lpParam; |
126 |
|
|
129 |
|
|
130 |
// debugging |
// debugging |
131 |
if (VERBOSE) { |
if (VERBOSE) { |
132 |
|
thread_id = GetCurrentThreadId(); |
133 |
fprintf(stdout, "----------------------------------------------\n"); |
fprintf(stdout, "----------------------------------------------\n"); |
134 |
fprintf(stdout, "thread_id: %i\n", thread_id); |
fprintf(stdout, "thread_id: %i\n", thread_id); |
135 |
fprintf(stdout, "arg.start_row: %i\n", args->start_row); |
fprintf(stdout, "arg.start_row: %i\n", args->start_row); |
141 |
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--) { |
142 |
//fprintf(stdout, "calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); |
//fprintf(stdout, "calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); |
143 |
for (x = 0; x < XSIZE; x++) { |
for (x = 0; x < XSIZE; x++) { |
144 |
|
|
145 |
|
// call to function in static linked library |
146 |
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]); |
147 |
|
|
148 |
// transfer color values to current pixel |
// transfer color values to current pixel |
206 |
// information for creating processes |
// information for creating processes |
207 |
STARTUPINFO si; |
STARTUPINFO si; |
208 |
PROCESS_INFORMATION pi; |
PROCESS_INFORMATION pi; |
209 |
char szCmdline[65536]; |
char szCmdline[MAX_COMMAND_LINE]; |
210 |
|
|
211 |
// command line stuff |
// command line stuff |
212 |
char arg_option[1024]; |
char arg_option[1024]; |
215 |
char *verbose_option = ""; |
char *verbose_option = ""; |
216 |
|
|
217 |
|
|
218 |
// parse command line arguments |
// check command line arguments |
219 |
if (argc < 2) { |
if (argc < 2) { |
220 |
fprintf(stderr, "Can not run without arguments!\nPlease specify '-t {number of threads}' or '-p {number of processes}' and an image filename.\n"); |
fprintf(stderr, "Error: Can not run without arguments!\nPlease specify '-t {number of threads}' or '-p {number of processes}' and an image filename.\n"); |
221 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
222 |
} |
} |
223 |
|
|
224 |
|
|
225 |
|
// parse command line arguments |
226 |
if (scan_argv(argc, argv, "--verbose", arg_value)) { |
if (scan_argv(argc, argv, "--verbose", arg_value)) { |
227 |
VERBOSE = TRUE; |
VERBOSE = TRUE; |
228 |
} |
} |
233 |
|
|
234 |
} else if (scan_argv(argc, argv, "-p", arg_value)) { |
} else if (scan_argv(argc, argv, "-p", arg_value)) { |
235 |
if (strlen(arg_value) == 0) { |
if (strlen(arg_value) == 0) { |
236 |
fprintf(stderr, "Please specify number of processes!\n"); |
fprintf(stderr, "Error: Please specify number of processes!\n"); |
237 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
238 |
} |
} |
239 |
use_processes = TRUE; |
use_processes = TRUE; |
241 |
workers = atoi(arg_value); |
workers = atoi(arg_value); |
242 |
|
|
243 |
} else if (scan_argv(argc, argv, "-t", arg_value)) { |
} else if (scan_argv(argc, argv, "-t", arg_value)) { |
244 |
if (strlen(arg_value) == 0) { |
|
245 |
fprintf(stderr, "Please specify number of threads!\n"); |
if (strlen(arg_value) == 0) |
246 |
|
fprintf(stderr, "Error: Please specify number of threads!\n"), |
247 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
248 |
} |
|
249 |
use_processes = FALSE; |
use_processes = FALSE; |
250 |
is_worker_process = FALSE; |
is_worker_process = FALSE; |
251 |
workers = atoi(arg_value); |
workers = atoi(arg_value); |
252 |
|
|
253 |
} |
} |
254 |
|
|
255 |
|
if (!is_worker_process && workers < 1) |
256 |
|
fprintf(stderr, "Error: Number of threads/processes must be >0!\n"), |
257 |
|
exit(EXIT_FAILURE); |
258 |
|
|
259 |
if (VERBOSE && use_processes) { |
if (VERBOSE && use_processes) { |
260 |
fprintf(stdout, "===================================================== "); |
fprintf(stdout, "===================================================== "); |
269 |
if (!is_worker_process) { |
if (!is_worker_process) { |
270 |
|
|
271 |
if (argc < 4) { |
if (argc < 4) { |
272 |
fprintf(stderr, "Must give filename of image as third argument!\n"); |
fprintf(stderr, "Error: Must give filename of image as third argument!\n"); |
273 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
274 |
} |
} |
275 |
|
|
286 |
} |
} |
287 |
|
|
288 |
// create the file mapping object |
// create the file mapping object |
289 |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal"); |
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, MMAP_NAME); |
290 |
if (NULL == hMap) { |
if (NULL == hMap) { |
291 |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
printErrorAndExit("Error at CreateFileMapping", GetLastError()); |
292 |
} |
} |
295 |
} else { |
} else { |
296 |
|
|
297 |
// open existing mapping object |
// open existing mapping object |
298 |
hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, "bmp_fractal"); |
hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, MMAP_NAME); |
299 |
if (NULL == hMap) |
if (NULL == hMap) |
300 |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
printErrorAndExit("Error at OpenFileMapping", GetLastError()); |
301 |
} |
} |
413 |
|
|
414 |
if (VERBOSE) |
if (VERBOSE) |
415 |
verbose_option = "--verbose"; |
verbose_option = "--verbose"; |
416 |
_snprintf(szCmdline, 1023, "%s %s %i %i %s", argv[0], "--worker", worker_startrow, worker_rows, verbose_option); |
_snprintf(szCmdline, MAX_COMMAND_LINE - 1, "%s %s %i %i %s", argv[0], "--worker", worker_startrow, worker_rows, verbose_option); |
417 |
if (VERBOSE) |
if (VERBOSE) |
418 |
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
fprintf(stdout, "starting worker process: %s\n", szCmdline); |
419 |
|
|
420 |
|
// prepare data structures for CreateProcess |
421 |
ZeroMemory( &si, sizeof(si) ); |
ZeroMemory( &si, sizeof(si) ); |
422 |
si.cb = sizeof(si); |
si.cb = sizeof(si); |
423 |
ZeroMemory( &pi, sizeof(pi) ); |
ZeroMemory( &pi, sizeof(pi) ); |
457 |
// perror("WaitForSingleObject"); |
// perror("WaitForSingleObject"); |
458 |
|
|
459 |
// close all worker handles |
// close all worker handles |
460 |
for (worker_index = 0; worker_index < workers; worker_index++) |
for (worker_index = 0; worker_index < workers; worker_index++) { |
461 |
CloseHandle(worker_handles[worker_index]); |
if (!CloseHandle(worker_handles[worker_index])) |
462 |
|
printErrorAndExit("Error at CloseHandle for worker handles", GetLastError()); |
463 |
|
} |
464 |
|
|
465 |
// write the result into the file |
// write the result into the file |
466 |
if (!FlushViewOfFile(pData, 0)) { |
if (!FlushViewOfFile(pData, 0)) |
467 |
err = GetLastError(); |
printErrorAndExit("Error at UnmapViewOfFile", GetLastError()); |
|
printErrorAndExit("Error at UnmapViewOfFile", err); |
|
|
} |
|
468 |
|
|
469 |
// remove the mapped file |
// remove the mapped file |
470 |
if (!UnmapViewOfFile(pData)) { |
if (!UnmapViewOfFile(pData)) |
471 |
err = GetLastError(); |
printErrorAndExit("Error at UnmapViewOfFile", GetLastError()); |
|
printErrorAndExit("Error at UnmapViewOfFile", err); |
|
|
exit(err); |
|
|
} |
|
472 |
|
|
473 |
// cleanup handles |
// cleanup handles |
474 |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) { |
if (!CloseHandle(hMap) || !CloseHandle(hFile) ) |
475 |
err = GetLastError(); |
printErrorAndExit("Error at CloseHandle for memory mapped file", GetLastError()); |
|
printErrorAndExit("Error at CloseHandle", err); |
|
|
} |
|
476 |
|
|
477 |
free(worker_args); |
free(worker_args); |
478 |
free(worker_handles); |
free(worker_handles); |