--- joko/Uni/BSArch/04/bmp_fractal.c 2006/07/01 13:56:54 1.6 +++ 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.6 2006/07/01 13:56:54 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 @@ -96,8 +98,8 @@ // arguments for each thread typedef struct _WorkerArguments { - unsigned int start_row; - unsigned int number_of_rows; + int start_row; + int number_of_rows; unsigned char * pBitmap; } WORKERARGS, *PWORKERARGS; @@ -107,6 +109,7 @@ // thread stuff int thread_id; PWORKERARGS args; + unsigned char *pDataBitmapSegment; // fractal calculation int x, y; @@ -116,31 +119,39 @@ // get worker arguments args = (PWORKERARGS)lpParam; - - 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); + + // 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--) { + //printf("calc: thread=%i; y=%i limits: %i,%i p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment); 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 - args->pBitmap[0] = bgr[0]; - args->pBitmap[1] = bgr[1]; - args->pBitmap[2] = bgr[2]; + pDataBitmapSegment[0] = bgr[0]; + pDataBitmapSegment[1] = bgr[1]; + pDataBitmapSegment[2] = bgr[2]; // move pointer to next pixel - args->pBitmap += 3; + pDataBitmapSegment += 3; } - //no padding required because 1500%4 =0 + //no padding required because 1500%4 =0 ??? } + if (VERBOSE) + printf("thread finished: %i\n", thread_id); return 0; } @@ -152,13 +163,12 @@ DWORD err; HANDLE hMap, hFile; LPVOID pData; - unsigned char *pDataBitmap, *pDataBitmapCurrent; + unsigned char *pDataBitmap; // workers - unsigned int workers = 3; - unsigned int worker_index, worker_rows, worker_startrow; + int workers = 50; + int worker_index, worker_rows, worker_startrow; HANDLE *worker_handles; - //struct WorkerArguments *worker_args; PWORKERARGS worker_args; @@ -189,46 +199,53 @@ 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 single thread + //if (worker_index == 1) + // continue; + // number of row to start for each worker worker_startrow = worker_index * worker_rows; // 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; @@ -238,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 @@ -247,9 +264,19 @@ } // wait for all threads + if (VERBOSE) + printf("waiting...\n"); if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED) perror("WaitForMultipleObjects"); - + + // debugging: just run single thread + //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED) + // perror("WaitForSingleObject"); + + // close all worker handles + for (worker_index = 0; worker_index < workers; worker_index++) + CloseHandle(worker_handles[worker_index]); + /* write the result into the file */ if (!FlushViewOfFile(pData, 0)) { err = GetLastError(); @@ -269,4 +296,9 @@ printErrorAndExit("Error at CloseHandle", err); } + free(worker_args); + free(worker_handles); + + return 0; + }