/[cvs]/joko/Uni/BSArch/04/bmp_fractal.c
ViewVC logotype

Diff of /joko/Uni/BSArch/04/bmp_fractal.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by joko, Sat Jul 1 13:56:54 2006 UTC revision 1.9 by joko, Sun Jul 2 00:24:17 2006 UTC
# Line 15  Line 15 
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
# Line 96  void write_blank_file(char *filename) { Line 98  void write_blank_file(char *filename) {
98    
99  // arguments for each thread  // arguments for each thread
100  typedef struct _WorkerArguments {  typedef struct _WorkerArguments {
101    unsigned int start_row;    int start_row;
102    unsigned int number_of_rows;    int number_of_rows;
103    unsigned char * pBitmap;    unsigned char * pBitmap;
104  } WORKERARGS, *PWORKERARGS;  } WORKERARGS, *PWORKERARGS;
105    
# Line 107  DWORD WINAPI fractal_thread (LPVOID lpPa Line 109  DWORD WINAPI fractal_thread (LPVOID lpPa
109    // thread stuff    // thread stuff
110    int thread_id;    int thread_id;
111    PWORKERARGS args;    PWORKERARGS args;
112      unsigned char *pDataBitmapSegment;
113    
114    // fractal calculation    // fractal calculation
115    int x, y;    int x, y;
# Line 116  DWORD WINAPI fractal_thread (LPVOID lpPa Line 119  DWORD WINAPI fractal_thread (LPVOID lpPa
119        
120    // get worker arguments    // get worker arguments
121    args = (PWORKERARGS)lpParam;    args = (PWORKERARGS)lpParam;
122      
123    printf("thread_id: %i\n", thread_id);    // calculate pointer to beginning of segment
124    printf("arg.start_row: %i\n", args->start_row);    pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE);
125    printf("arg.number_of_rows: %i\n", args->number_of_rows);  
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--) {
137        //printf("calc: thread=%i; y=%i            limits: %i,%i    p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment);
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        args->pBitmap[0] = bgr[0];        pDataBitmapSegment[0] = bgr[0];
143        args->pBitmap[1] = bgr[1];        pDataBitmapSegment[1] = bgr[1];
144        args->pBitmap[2] = bgr[2];        pDataBitmapSegment[2] = bgr[2];
145                
146        // move pointer to next pixel        // move pointer to next pixel
147        args->pBitmap += 3;        pDataBitmapSegment += 3;
148                
149      }      }
150      //no padding required because 1500%4 =0      //no padding required because 1500%4 =0 ???
151    }    }
152    
153      if (VERBOSE)
154        printf("thread finished: %i\n", thread_id);
155    return 0;    return 0;
156    
157  }  }
# Line 152  int main(int argc, char *argv[]) { Line 163  int main(int argc, char *argv[]) {
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    unsigned int workers = 3;    int workers = 50;
170    unsigned 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        
# Line 189  int main(int argc, char *argv[]) { Line 199  int main(int argc, char *argv[]) {
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 single thread
238        //if (worker_index == 1)
239        //  continue;
240        
241      // number of row to start for each worker      // number of row to start for each worker
242      worker_startrow = worker_index * worker_rows;      worker_startrow = worker_index * worker_rows;
243            
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;
# Line 238  int main(int argc, char *argv[]) { Line 255  int main(int argc, char *argv[]) {
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
# Line 247  int main(int argc, char *argv[]) { Line 264  int main(int argc, char *argv[]) {
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 single thread
273      //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED)
274      //  perror("WaitForSingleObject");
275    
276      // close all worker handles
277      for (worker_index = 0; worker_index < workers; worker_index++)
278        CloseHandle(worker_handles[worker_index]);
279        
280    /* write the result into the file */    /* write the result into the file */
281    if (!FlushViewOfFile(pData, 0)) {    if (!FlushViewOfFile(pData, 0)) {
282      err = GetLastError();      err = GetLastError();
# Line 269  int main(int argc, char *argv[]) { Line 296  int main(int argc, char *argv[]) {
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  }  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.9

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed