--- joko/Uni/BSArch/04/bmp_fractal.c 2006/07/01 20:31:31 1.7 +++ joko/Uni/BSArch/04/bmp_fractal.c 2006/07/02 00:24:17 1.9 @@ -5,7 +5,7 @@ * Uebung 4.4 */ -// $Id: bmp_fractal.c,v 1.7 2006/07/01 20:31:31 joko Exp $ +// $Id: bmp_fractal.c,v 1.9 2006/07/02 00:24:17 joko Exp $ #include #include @@ -15,6 +15,8 @@ #define YSIZE 500 #include "algorithm.h" +BOOL VERBOSE = FALSE; + /* BMP Header */ unsigned char header[54]={0x42,0x4d, // signature BM @@ -117,15 +119,18 @@ // get worker arguments 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); // calculate pointer to beginning of segment pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE); - printf("segment_start: %p\n", pDataBitmapSegment); + + // debugging + if (VERBOSE) { + 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); + printf("segment_start: %p\n", pDataBitmapSegment); + } // calculate fractal for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) { @@ -133,9 +138,6 @@ for (x = 0; x < XSIZE; x++) { getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]); - // debugging - //printf("pointer: %p\n", pDataBitmapCurrent); - // transfer color values to current pixel pDataBitmapSegment[0] = bgr[0]; pDataBitmapSegment[1] = bgr[1]; @@ -145,10 +147,11 @@ pDataBitmapSegment += 3; } - //no padding required because 1500%4 =0 + //no padding required because 1500%4 =0 ??? } - printf("thread finished: %i\n", thread_id); + if (VERBOSE) + printf("thread finished: %i\n", thread_id); return 0; } @@ -160,13 +163,12 @@ DWORD err; HANDLE hMap, hFile; LPVOID pData; - unsigned char *pDataBitmap, *pDataBitmapCurrent; + unsigned char *pDataBitmap; // workers - int workers = 10; + int workers = 50; int worker_index, worker_rows, worker_startrow; HANDLE *worker_handles; - //struct WorkerArguments *worker_args; PWORKERARGS worker_args; @@ -197,40 +199,42 @@ pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header)); // debugging - printf("pos. of file: %p\n", pData); - printf("pos. of bitmap: %p\n", pDataBitmap); - - // pointer to current pixel - pDataBitmapCurrent = pDataBitmap; + if (VERBOSE) { + printf("pos. of file: %p\n", pData); + printf("pos. of bitmap: %p\n", pDataBitmap); + } /* - // turn bitmap into white wand + // turn bitmap into white canvas for (offset = 0; offset < 500 * 500 * 3; offset++) { - //pDataBitmap[offset] = 255; - *pDataBitmapCurrent = 255; - pDataBitmapCurrent++; + *pDataBitmap = 255; + pDataBitmap++; } + exit(0); */ - + // allocate memory for bitmap + /* + if ((pDataBitmap = malloc(XSIZE * YSIZE * 3 * sizeof(pDataBitmap[0]))) == NULL) + perror("malloc"), exit(1); + */ + // allocate memory for table of all worker handles - if ((worker_handles = malloc(workers * sizeof worker_handles[0])) == NULL) + if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL) perror("malloc"), exit(1); - /* // allocate memory for table of all worker arguments - if ((worker_args = malloc(workers * sizeof worker_args[0])) == NULL) + if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL) perror("malloc"), exit(1); - */ - // allocate memory for table of all worker arguments - worker_args = (PWORKERARGS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, workers * sizeof(WORKERARGS)); + // calculate segments of bitmap for worker threads/processes and start them worker_rows = YSIZE / workers; - printf("rows for each worker: %i\n", worker_rows); + if (VERBOSE) + printf("rows for each worker: %i\n", worker_rows); for (worker_index = 0; worker_index < workers; worker_index++) { - // debugging: just run with single thread + // debugging: just run single thread //if (worker_index == 1) // continue; @@ -240,7 +244,8 @@ // recalculate number of rows for last worker if (YSIZE mod workers) != 0 if (worker_index == workers - 1) { worker_rows = YSIZE - worker_startrow; - printf("rows for last worker: %i\n", worker_rows); + if (VERBOSE) + printf("rows for last worker: %i\n", worker_rows); } worker_args[worker_index].start_row = worker_startrow; @@ -250,7 +255,7 @@ worker_handles[worker_index] = CreateThread( NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes, 0, // SIZE_T dwStackSize, - fractal_thread, // LPTHREAD_START_ROUTINE lpStartAddress, + &fractal_thread, // LPTHREAD_START_ROUTINE lpStartAddress, &worker_args[worker_index], // LPVOID lpParameter, 0, // DWORD dwCreationFlags, NULL // LPDWORD lpThreadId @@ -259,10 +264,12 @@ } // wait for all threads + if (VERBOSE) + printf("waiting...\n"); if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) perror("WaitForMultipleObjects"); - // debugging: just run with single thread + // debugging: just run single thread //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) // perror("WaitForSingleObject"); @@ -289,4 +296,9 @@ printErrorAndExit("Error at CloseHandle", err); } + free(worker_args); + free(worker_handles); + + return 0; + }