/[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.9 by joko, Sun Jul 2 00:24:17 2006 UTC revision 1.11 by joko, Sun Jul 2 02:12:27 2006 UTC
# Line 15  Line 15 
15  #define YSIZE 500  #define YSIZE 500
16  #include "algorithm.h"  #include "algorithm.h"
17    
18    #define VERBOSE_HANDLE "stderr";
19    
20  BOOL VERBOSE = FALSE;  BOOL VERBOSE = FALSE;
21    
22    
# Line 49  void printErrorAndExit(const char *msg, Line 51  void printErrorAndExit(const char *msg,
51                                            0,                                            0,
52                                            NULL ))                                            NULL ))
53          {          {
54                  fprintf(stderr,"%s : %s\n",msg,lpMsgBuf);                  fprintf(stdout, "%s: %s\n", msg, lpMsgBuf);
55                  LocalFree(lpMsgBuf);                  LocalFree(lpMsgBuf);
56          }          }
57          else          else
58          {          {
59                  fprintf(stderr,"Error at FormatMesage: %d\n",err=GetLastError());                  fprintf(stdout, "Error at FormatMesage: %d\n",err=GetLastError());
60          }          }
61          exit(err);          exit(err);
62  }  }
# Line 104  typedef struct _WorkerArguments { Line 106  typedef struct _WorkerArguments {
106  } WORKERARGS, *PWORKERARGS;  } WORKERARGS, *PWORKERARGS;
107    
108  // worker thread - main entry function  // worker thread - main entry function
109  DWORD WINAPI fractal_thread (LPVOID lpParam) {  DWORD WINAPI fractal_create_segment (LPVOID lpParam) {
110        
111    // thread stuff    // thread stuff
112    int thread_id;    int thread_id;
# Line 125  DWORD WINAPI fractal_thread (LPVOID lpPa Line 127  DWORD WINAPI fractal_thread (LPVOID lpPa
127    
128    // debugging    // debugging
129    if (VERBOSE) {    if (VERBOSE) {
130      printf("----------------------------------------------\n");      fprintf(stdout, "----------------------------------------------\n");
131      printf("thread_id: %i\n", thread_id);      fprintf(stdout, "thread_id: %i\n", thread_id);
132      printf("arg.start_row: %i\n", args->start_row);      fprintf(stdout, "arg.start_row: %i\n", args->start_row);
133      printf("arg.number_of_rows: %i\n", args->number_of_rows);      fprintf(stdout, "arg.number_of_rows: %i\n", args->number_of_rows);
134      printf("segment_start: %p\n", pDataBitmapSegment);      fprintf(stdout, "segment_start: %p\n", pDataBitmapSegment);
135    }    }
136        
137    // calculate fractal    // calculate fractal
138    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--) {
139      //printf("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);
140      for (x = 0; x < XSIZE; x++) {      for (x = 0; x < XSIZE; x++) {
141        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]);
142                
# Line 151  DWORD WINAPI fractal_thread (LPVOID lpPa Line 153  DWORD WINAPI fractal_thread (LPVOID lpPa
153    }    }
154    
155    if (VERBOSE)    if (VERBOSE)
156      printf("thread finished: %i\n", thread_id);      fprintf(stdout, "thread finished: %i\n", thread_id);
157    return 0;    return 0;
158    
159  }  }
# Line 166  int main(int argc, char *argv[]) { Line 168  int main(int argc, char *argv[]) {
168    unsigned char *pDataBitmap;    unsigned char *pDataBitmap;
169    
170    // workers    // workers
171    int workers = 50;    int workers = 5;
172    int worker_index, worker_rows, worker_startrow;    int worker_index, worker_rows, worker_startrow;
173    HANDLE *worker_handles;    HANDLE *worker_handles;
174    PWORKERARGS worker_args;    PWORKERARGS worker_args;
175    
176      // threads or processes?
177      BOOL use_processes = TRUE;
178      BOOL is_worker = FALSE;
179      
180      // information for creating processes
181      STARTUPINFO si;
182      PROCESS_INFORMATION pi;
183      char szCmdline[1024];
184      
185      
186      VERBOSE = TRUE;
187      
188      // "parse" command line arguments
189      if (argc >= 2) {
190        if (strcmp(argv[1], "--worker") == 0) {
191          is_worker = TRUE;
192        }
193      }
194      
195      if (VERBOSE && use_processes) {
196        fprintf(stdout, "===================================================== ");
197        if (is_worker)
198          fprintf(stdout, "WORKER-PROCESS\n");
199        else
200          fprintf(stdout, "MASTER-PROCESS\n");
201      }
202        
203    // create empty bmp-file (black background)    // create empty bmp-file (black background)
204    write_blank_file("test.bmp");    if (!is_worker)
205        write_blank_file("test.bmp");
206    
207    /* open file for reading and writing */    if (!is_worker) {
208    hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);      
209    if (INVALID_HANDLE_VALUE == hFile) {      /* open file for reading and writing */
210      err = GetLastError();      hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
211      printErrorAndExit("Error at CreateFile",err);      if (INVALID_HANDLE_VALUE == hFile) {
212    }        err = GetLastError();
213          printErrorAndExit("Error at CreateFile",err);
214        }
215    
216    /* create the file mapping object */      /* create the file mapping object */
217    hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);      hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, "bmp_fractal");
218    if (NULL == hMap) {      if (NULL == hMap) {
219      printErrorAndExit("Error at CreateFileMapping", GetLastError());        printErrorAndExit("Error at CreateFileMapping", GetLastError());
220        }
221        
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 */
# Line 200  int main(int argc, char *argv[]) { Line 239  int main(int argc, char *argv[]) {
239        
240    // debugging    // debugging
241    if (VERBOSE) {    if (VERBOSE) {
242      printf("pos. of file: %p\n", pData);      fprintf(stdout, "pos. of file: %p\n", pData);
243      printf("pos. of bitmap: %p\n", pDataBitmap);      fprintf(stdout, "pos. of bitmap: %p\n", pDataBitmap);
244    }    }
245    
246      
247      if (use_processes && is_worker) {  
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        return 0;
268      }
269      
270    /*    /*
271    // turn bitmap into white canvas    // turn bitmap into white canvas
272    for (offset = 0; offset < 500 * 500 * 3; offset++) {    for (offset = 0; offset < 500 * 500 * 3; offset++) {
# Line 228  int main(int argc, char *argv[]) { Line 291  int main(int argc, char *argv[]) {
291      perror("malloc"), exit(1);      perror("malloc"), exit(1);
292        
293        
294    // calculate segments of bitmap for worker threads/processes and start them    // calculate bitmap segment length for workers
295    worker_rows = YSIZE / workers;    worker_rows = YSIZE / workers;
296    if (VERBOSE)    if (VERBOSE)
297      printf("rows for each worker: %i\n", worker_rows);      fprintf(stdout, "rows for each worker: %i\n", worker_rows);
298      
299      // start workers
300    for (worker_index = 0; worker_index < workers; worker_index++) {    for (worker_index = 0; worker_index < workers; worker_index++) {
301            
302      // debugging: just run single thread      // debugging: just run single thread
# Line 245  int main(int argc, char *argv[]) { Line 310  int main(int argc, char *argv[]) {
310      if (worker_index == workers - 1) {      if (worker_index == workers - 1) {
311        worker_rows = YSIZE - worker_startrow;        worker_rows = YSIZE - worker_startrow;
312        if (VERBOSE)        if (VERBOSE)
313          printf("rows for last worker: %i\n", worker_rows);          fprintf(stdout, "rows for last worker: %i\n", worker_rows);
314      }      }
315            
316        // assign each worker's arguments
317      worker_args[worker_index].start_row = worker_startrow;      worker_args[worker_index].start_row = worker_startrow;
318      worker_args[worker_index].number_of_rows = worker_rows;      worker_args[worker_index].number_of_rows = worker_rows;
319      worker_args[worker_index].pBitmap = pDataBitmap;      worker_args[worker_index].pBitmap = pDataBitmap;
320            
321      worker_handles[worker_index] = CreateThread(      if (!use_processes) {
       NULL,                          // LPSECURITY_ATTRIBUTES lpThreadAttributes,  
       0,                             // SIZE_T dwStackSize,  
       &fractal_thread,                // LPTHREAD_START_ROUTINE lpStartAddress,  
       &worker_args[worker_index],    // LPVOID lpParameter,  
       0,                             // DWORD dwCreationFlags,  
       NULL                           // LPDWORD lpThreadId  
     );  
322            
323          worker_handles[worker_index] = CreateThread(
324            NULL,                          // LPSECURITY_ATTRIBUTES lpThreadAttributes,
325            0,                             // SIZE_T dwStackSize,
326            &fractal_create_segment,                // LPTHREAD_START_ROUTINE lpStartAddress,
327            &worker_args[worker_index],    // LPVOID lpParameter,
328            0,                             // DWORD dwCreationFlags,
329            NULL                           // LPDWORD lpThreadId
330          );
331    
332          if (!worker_handles[worker_index])
333            printErrorAndExit("CreateThread failed", GetLastError());
334          
335        } else {
336    
337          _snprintf(szCmdline, 1023, "%s %s %i %i", argv[0], "--worker", worker_startrow, worker_rows);
338          if (VERBOSE)
339            fprintf(stdout, "starting worker process: %s\n", szCmdline);
340          ZeroMemory( &si, sizeof(si) );
341          si.cb = sizeof(si);
342          ZeroMemory( &pi, sizeof(pi) );
343          
344          if (!CreateProcess(
345              NULL,           // No module name (use command line)
346              szCmdline,      // Command line
347              NULL,           // Process handle not inheritable
348              NULL,           // Thread handle not inheritable
349              FALSE,          // Set handle inheritance to FALSE
350              0,              // No creation flags
351              NULL,           // Use parent's environment block
352              NULL,           // Use parent's starting directory
353              &si,            // Pointer to STARTUPINFO structure
354              &pi             // Pointer to PROCESS_INFORMATION structure
355          ))
356            printErrorAndExit("CreateProcess failed", GetLastError());
357        
358          worker_handles[worker_index] = pi.hProcess;
359          
360        }
361    
362    }    }
363    
364    // wait for all threads    // wait for all threads
365    if (VERBOSE)    if (VERBOSE)
366      printf("waiting...\n");      fprintf(stdout, "waiting for workers to finish...\n");
367    if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED)    if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED)
368      perror("WaitForMultipleObjects");      perror("WaitForMultipleObjects");
369    

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

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