/[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.12 by joko, Sun Jul 2 09:27:14 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 47  void printErrorAndExit(const char *msg, Line 49  void printErrorAndExit(const char *msg,
49                                            0,                                            0,
50                                            NULL ))                                            NULL ))
51          {          {
52                  fprintf(stderr,"%s : %s\n",msg,lpMsgBuf);                  fprintf(stdout, "%s: %s\n", msg, lpMsgBuf);
53                  LocalFree(lpMsgBuf);                  LocalFree(lpMsgBuf);
54          }          }
55          else          else
56          {          {
57                  fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError());                  fprintf(stdout, "Error at FormatMesage: %d\n",err=GetLastError());
58          }          }
59          exit(err);          exit(err);
60  }  }
# 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    
106  // worker thread - main entry function  // worker thread - main entry function
107  DWORD WINAPI fractal_thread (LPVOID lpParam) {  DWORD WINAPI fractal_create_segment (LPVOID lpParam) {
108        
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        fprintf(stdout, "----------------------------------------------\n");
129        fprintf(stdout, "thread_id: %i\n", thread_id);
130        fprintf(stdout, "arg.start_row: %i\n", args->start_row);
131        fprintf(stdout, "arg.number_of_rows: %i\n", args->number_of_rows);
132        fprintf(stdout, "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        //fprintf(stdout, "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        fprintf(stdout, "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 = 5;
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      // threads or processes?
175      BOOL use_processes = TRUE;
176      BOOL is_worker_process = FALSE;
177      
178      // information for creating processes
179      STARTUPINFO si;
180      PROCESS_INFORMATION pi;
181      char szCmdline[1024];
182      
183      
184      VERBOSE = TRUE;
185      
186      // "parse" command line arguments
187      if (argc >= 2) {
188        if (strcmp(argv[1], "--worker") == 0) {
189          is_worker_process = TRUE;
190        }
191      }
192      
193      if (VERBOSE && use_processes) {
194        fprintf(stdout, "===================================================== ");
195        if (is_worker_process)
196          fprintf(stdout, "WORKER-PROCESS\n");
197        else
198          fprintf(stdout, "MASTER-PROCESS\n");
199      }
200        
201    // create empty bmp-file (black background)    // create empty bmp-file (black background)
202    write_blank_file("test.bmp");    if (!is_worker_process)
203        write_blank_file("test.bmp");
204    
205    /* open file for reading and writing */    // master creates memory mapped file
206    hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);    if (!is_worker_process) {
207    if (INVALID_HANDLE_VALUE == hFile) {      
208      err = GetLastError();      // open file for reading and writing
209      printErrorAndExit("Error at CreateFile",err);      hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
210    }      if (INVALID_HANDLE_VALUE == hFile) {
211          err = GetLastError();
212          printErrorAndExit("Error at CreateFile",err);
213        }
214    
215    /* create the file mapping object */      // create the file mapping object
216    hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);      hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal");
217    if (NULL == hMap) {      if (NULL == hMap) {
218      printErrorAndExit("Error at CreateFileMapping", GetLastError());        printErrorAndExit("Error at CreateFileMapping", GetLastError());
219        }
220      
221      // worker uses existing memory mapped file
222      } else {
223        
224        // open existing mapping object
225        hMap = OpenFileMapping(FILE_MAP_WRITE, FALSE, "bmp_fractal");
226        if (NULL == hMap)
227          printErrorAndExit("Error at OpenFileMapping", GetLastError());
228    }    }
229        
230    /* map the whole file into the process context */    // map the whole file into the process context
231    pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);    pData = MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, 0);
232    if (NULL == pData) {    if (NULL == pData) {
233      printErrorAndExit("Error at MapViewOfFile", GetLastError());      printErrorAndExit("Error at MapViewOfFile", GetLastError());
# Line 189  int main(int argc, char *argv[]) { Line 238  int main(int argc, char *argv[]) {
238    pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header));    pDataBitmap = (unsigned char *)((INT_PTR)pData + sizeof(header));
239        
240    // debugging    // debugging
241    printf("pos. of file: %p\n", pData);    if (VERBOSE) {
242    printf("pos. of bitmap: %p\n", pDataBitmap);      fprintf(stdout, "pos. of file: %p\n", pData);
243        fprintf(stdout, "pos. of bitmap: %p\n", pDataBitmap);
244    // pointer to current pixel    }
   pDataBitmapCurrent = pDataBitmap;  
245    
246      
247      if (use_processes && is_worker_process) {
248      
249        if (VERBOSE)
250          fprintf(stdout, "inside worker-process\n");
251        
252        // get segment information from command line
253        worker_startrow = atoi(argv[2]);
254        worker_rows = atoi(argv[3]);
255        
256        // allocate memory for one worker's arguments
257        if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL)
258          perror("malloc"), exit(1);
259        
260        // assign worker's arguments
261        worker_args[0].start_row = worker_startrow;
262        worker_args[0].number_of_rows = worker_rows;
263        worker_args[0].pBitmap = pDataBitmap;
264    
265        fractal_create_segment(&worker_args[0]);
266    
267        // cleanup mmap-handle
268        if (!CloseHandle(hMap))
269          printErrorAndExit("Error at CloseHandle", GetLastError());
270        
271        return 0;
272      }
273      
274    /*    /*
275    // turn bitmap into white wand    // turn bitmap into white canvas
276    for (offset = 0; offset < 500 * 500 * 3; offset++) {    for (offset = 0; offset < 500 * 500 * 3; offset++) {
277      //pDataBitmap[offset] = 255;      *pDataBitmap = 255;
278      *pDataBitmapCurrent = 255;      pDataBitmap++;
     pDataBitmapCurrent++;  
279    }    }
280      exit(0);
281    */    */
282    
283      // allocate memory for bitmap
284      /*
285      if ((pDataBitmap = malloc(XSIZE * YSIZE * 3 * sizeof(pDataBitmap[0]))) == NULL)
286        perror("malloc"), exit(1);
287      */
288      
289    // allocate memory for table of all worker handles    // allocate memory for table of all worker handles
290    if ((worker_handles = malloc(workers * sizeof worker_handles[0])) == NULL)    if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL)
291      perror("malloc"), exit(1);      perror("malloc"), exit(1);
292    
   /*  
293    // allocate memory for table of all worker arguments    // allocate memory for table of all worker arguments
294    if ((worker_args = malloc(workers * sizeof worker_args[0])) == NULL)    if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL)
295      perror("malloc"), exit(1);      perror("malloc"), exit(1);
   */  
   // allocate memory for table of all worker arguments  
   worker_args = (PWORKERARGS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, workers * sizeof(WORKERARGS));  
296        
297    // calculate segments of bitmap for worker threads/processes and start them    
298      // calculate bitmap segment length for workers
299    worker_rows = YSIZE / workers;    worker_rows = YSIZE / workers;
300    printf("rows for each worker: %i\n", worker_rows);    if (VERBOSE)
301        fprintf(stdout, "rows for each worker: %i\n", worker_rows);
302      
303      // start workers
304    for (worker_index = 0; worker_index < workers; worker_index++) {    for (worker_index = 0; worker_index < workers; worker_index++) {
305            
306        // debugging: just run single thread
307        //if (worker_index == 1)
308        //  continue;
309        
310      // number of row to start for each worker      // number of row to start for each worker
311      worker_startrow = worker_index * worker_rows;      worker_startrow = worker_index * worker_rows;
312            
313      // recalculate number of rows for last worker if (YSIZE mod workers) != 0      // recalculate number of rows for last worker if (YSIZE mod workers) != 0
314      if (worker_index == workers - 1) {      if (worker_index == workers - 1) {
315        worker_rows = YSIZE - worker_startrow;        worker_rows = YSIZE - worker_startrow;
316        printf("rows for last worker: %i\n", worker_rows);        if (VERBOSE)
317            fprintf(stdout, "rows for last worker: %i\n", worker_rows);
318      }      }
319            
320        // assign each worker's arguments
321      worker_args[worker_index].start_row = worker_startrow;      worker_args[worker_index].start_row = worker_startrow;
322      worker_args[worker_index].number_of_rows = worker_rows;      worker_args[worker_index].number_of_rows = worker_rows;
323      worker_args[worker_index].pBitmap = pDataBitmap;      worker_args[worker_index].pBitmap = pDataBitmap;
324            
325      worker_handles[worker_index] = CreateThread(      if (!use_processes) {
326        NULL,                          // LPSECURITY_ATTRIBUTES lpThreadAttributes,      
327        0,                             // SIZE_T dwStackSize,        worker_handles[worker_index] = CreateThread(
328        fractal_thread,                // LPTHREAD_START_ROUTINE lpStartAddress,          NULL,                          // LPSECURITY_ATTRIBUTES lpThreadAttributes,
329        &worker_args[worker_index],    // LPVOID lpParameter,          0,                             // SIZE_T dwStackSize,
330        0,                             // DWORD dwCreationFlags,          &fractal_create_segment,                // LPTHREAD_START_ROUTINE lpStartAddress,
331        NULL                           // LPDWORD lpThreadId          &worker_args[worker_index],    // LPVOID lpParameter,
332      );          0,                             // DWORD dwCreationFlags,
333            NULL                           // LPDWORD lpThreadId
334          );
335    
336          if (!worker_handles[worker_index])
337            printErrorAndExit("CreateThread failed", GetLastError());
338          
339        } else {
340    
341          _snprintf(szCmdline, 1023, "%s %s %i %i", argv[0], "--worker", worker_startrow, worker_rows);
342          if (VERBOSE)
343            fprintf(stdout, "starting worker process: %s\n", szCmdline);
344          ZeroMemory( &si, sizeof(si) );
345          si.cb = sizeof(si);
346          ZeroMemory( &pi, sizeof(pi) );
347          
348          if (!CreateProcess(
349              NULL,           // No module name (use command line)
350              szCmdline,      // Command line
351              NULL,           // Process handle not inheritable
352              NULL,           // Thread handle not inheritable
353              FALSE,          // Set handle inheritance to FALSE
354              0,              // No creation flags
355              NULL,           // Use parent's environment block
356              NULL,           // Use parent's starting directory
357              &si,            // Pointer to STARTUPINFO structure
358              &pi             // Pointer to PROCESS_INFORMATION structure
359          ))
360            printErrorAndExit("CreateProcess failed", GetLastError());
361            
362          worker_handles[worker_index] = pi.hProcess;
363          
364        }
365    
366    }    }
367    
368    // wait for all threads    // wait for all threads
369      if (VERBOSE)
370        fprintf(stdout, "waiting for workers to finish...\n");
371    if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED)    if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED)
372      perror("WaitForMultipleObjects");      perror("WaitForMultipleObjects");
373      
374    /* write the result into the file */    // debugging: just run single thread
375      //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED)
376      //  perror("WaitForSingleObject");
377    
378      // close all worker handles
379      for (worker_index = 0; worker_index < workers; worker_index++)
380        CloseHandle(worker_handles[worker_index]);
381        
382      // write the result into the file
383    if (!FlushViewOfFile(pData, 0)) {    if (!FlushViewOfFile(pData, 0)) {
384      err = GetLastError();      err = GetLastError();
385      printErrorAndExit("Error at UnmapViewOfFile", err);      printErrorAndExit("Error at UnmapViewOfFile", err);
386    }    }
387    
388    /* remove the mapped file */    // remove the mapped file
389    if (!UnmapViewOfFile(pData)) {    if (!UnmapViewOfFile(pData)) {
390      err = GetLastError();      err = GetLastError();
391      printErrorAndExit("Error at UnmapViewOfFile", err);      printErrorAndExit("Error at UnmapViewOfFile", err);
392      exit(err);      exit(err);
393    }    }
394        
395    /* cleanup handles */    // cleanup handles
396    if (!CloseHandle(hMap) || !CloseHandle(hFile) ) {    if (!CloseHandle(hMap) || !CloseHandle(hFile) ) {
397      err = GetLastError();      err = GetLastError();
398      printErrorAndExit("Error at CloseHandle", err);      printErrorAndExit("Error at CloseHandle", err);
399    }    }
400    
401      free(worker_args);
402      free(worker_handles);
403      
404      return 0;
405      
406  }  }

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

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