/[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.13 by joko, Sun Jul 2 10:45:02 2006 UTC revision 1.16 by joko, Sun Jul 2 12:21:01 2006 UTC
# Line 49  void printErrorAndExit(const char *msg, Line 49  void printErrorAndExit(const char *msg,
49                                            0,                                            0,
50                                            NULL ))                                            NULL ))
51          {          {
52                  fprintf(stdout, "%s: %s\n", msg, lpMsgBuf);                  fprintf(stderr, "%s: %s\n", msg, lpMsgBuf);
53                  LocalFree(lpMsgBuf);                  LocalFree(lpMsgBuf);
54          }          }
55          else          else
56          {          {
57                  fprintf(stdout, "Error at FormatMesage: %d\n",err=GetLastError());                  fprintf(stderr, "Error at FormatMesage: %d\n",err=GetLastError());
58          }          }
59          exit(err);          exit(err);
60  }  }
# Line 68  void write_blank_file(char *filename) { Line 68  void write_blank_file(char *filename) {
68    // open file handle    // open file handle
69    fd = fopen(filename, "wb+");    fd = fopen(filename, "wb+");
70    if (NULL == fd) {    if (NULL == fd) {
71      perror("open");      perror("Error while opening file for writing");
72      exit(1);      exit(1);
73    }    }
74    
# Line 77  void write_blank_file(char *filename) { Line 77  void write_blank_file(char *filename) {
77        
78    // error checking    // error checking
79    if (-1 == len || len != sizeof(header)) {    if (-1 == len || len != sizeof(header)) {
80      perror("write");      perror("Error while writing header to file");
81      exit(2);      exit(2);
82    }    }
83        
# Line 86  void write_blank_file(char *filename) { Line 86  void write_blank_file(char *filename) {
86    for (i = 0; i < img_size; i++) {    for (i = 0; i < img_size; i++) {
87      len = fwrite("\0\0\0", 1, 3, fd);      len = fwrite("\0\0\0", 1, 3, fd);
88      if (-1 == len || len != 3) {      if (-1 == len || len != 3) {
89        perror("write");        perror("Error while writing data to file");
90        exit(4);        exit(4);
91      }      }
92    }    }
# Line 156  DWORD WINAPI fractal_create_segment (LPV Line 156  DWORD WINAPI fractal_create_segment (LPV
156    
157  }  }
158    
159  BOOL scan_argv(int argc, char *argv[], char opt_name[], char *opt_value[]) {  BOOL scan_argv(int argc, char *argv[], char opt_name[], char *opt_value) {
160    int i;    int i;
161    char * opt_current_name;    char * opt_current_name;
162    char * opt_current_value;    char * opt_current_value;
163      
164    //printf("searching for: '%s'\n", opt_name);    //printf("searching for: '%s'\n", opt_name);
165        
166    for (i = 1; i < argc; i++) {    for (i = 1; i < argc; i++) {
167      opt_current_name = argv[i];      opt_current_name = argv[i];
168      if (strcmp(opt_current_name, opt_name) == 0) {      if (strcmp(opt_current_name, opt_name) == 0) {
       //printf("opt: %s\n", opt_current_name);  
169        opt_current_value = argv[i+1];        opt_current_value = argv[i+1];
       //printf("opt-current-value: %s\n", opt_current_value);  
170        if (opt_current_value != NULL) {        if (opt_current_value != NULL) {
171          //*opt_value = *argv[i+1];          strcpy(opt_value, opt_current_value);
         *opt_value = *opt_current_value;  
         //printf("opt-value: %s\n", opt_value);  
172        }        }
173        return TRUE;        return TRUE;
174      }      }
# Line 190  int main(int argc, char *argv[]) { Line 186  int main(int argc, char *argv[]) {
186    unsigned char *pDataBitmap;    unsigned char *pDataBitmap;
187    
188    // workers    // workers
189    int workers = 5;    int workers;
190    int worker_index, worker_rows, worker_startrow;    int worker_index, worker_rows, worker_startrow;
191    HANDLE *worker_handles;    HANDLE *worker_handles;
192    PWORKERARGS worker_args;    PWORKERARGS worker_args;
193      int worker_count;
194    
195    // threads or processes?    // threads or processes?
196    BOOL use_processes = FALSE;    BOOL use_processes = FALSE;
# Line 206  int main(int argc, char *argv[]) { Line 203  int main(int argc, char *argv[]) {
203        
204    // command line stuff    // command line stuff
205    char arg_option[1024];    char arg_option[1024];
206    char *arg_value[1024];    char arg_value[1024];
207    //arg_value = malloc(1024);    char *bmp_filename;
208      char *verbose_option = "";
209        
210    VERBOSE = FALSE;  
211      // parse command line arguments
212      if (argc < 2) {
213        fprintf(stderr, "Can not run without arguments!\nPlease specify '-t {number of threads}' or '-p {number of processes}' and an image filename.\n");
214        exit(EXIT_FAILURE);
215      }
216        
217    // "parse" command line arguments    if (scan_argv(argc, argv, "--verbose", arg_value)) {
218    /*      VERBOSE = TRUE;
   if (argc >= 2) {  
     if (strcmp(argv[1], "--worker") == 0) {  
       is_worker_process = TRUE;  
     }  
219    }    }
   */  
220        
   //scan_argv(argc, argv, "--worker", &arg_value);  
   //*arg_value = '\0';  
   // parse command line arguments  
221    if (scan_argv(argc, argv, "--worker", arg_value)) {    if (scan_argv(argc, argv, "--worker", arg_value)) {
222      use_processes = TRUE;      use_processes = TRUE;
223      is_worker_process = TRUE;      is_worker_process = TRUE;
224        
225    } else if (scan_argv(argc, argv, "-p", arg_value)) {    } else if (scan_argv(argc, argv, "-p", arg_value)) {
226        if (strlen(arg_value) == 0) {
227          fprintf(stderr, "Please specify number of processes!\n");
228          exit(EXIT_FAILURE);
229        }
230      use_processes = TRUE;      use_processes = TRUE;
231      is_worker_process = FALSE;      is_worker_process = FALSE;
232      workers = atoi(arg_value);      workers = atoi(arg_value);
233        
234    } else if (scan_argv(argc, argv, "-t", arg_value)) {    } else if (scan_argv(argc, argv, "-t", arg_value)) {
235        if (strlen(arg_value) == 0) {
236          fprintf(stderr, "Please specify number of threads!\n");
237          exit(EXIT_FAILURE);
238        }
239      use_processes = FALSE;      use_processes = FALSE;
240      is_worker_process = FALSE;      is_worker_process = FALSE;
241      workers = atoi(arg_value);      workers = atoi(arg_value);
242            
243    }    }
   //printf("value: %s\n", arg_value);  
   //exit(0);  
244        
245        
246    if (VERBOSE && use_processes) {    if (VERBOSE && use_processes) {
# Line 249  int main(int argc, char *argv[]) { Line 250  int main(int argc, char *argv[]) {
250      else      else
251        fprintf(stdout, "MASTER-PROCESS\n");        fprintf(stdout, "MASTER-PROCESS\n");
252    }    }
     
   // create empty bmp-file (black background)  
   if (!is_worker_process)  
     write_blank_file("test.bmp");  
253    
254    // master creates memory mapped file    
255      // master creates memory mapped file ("empty" image)
256    if (!is_worker_process) {    if (!is_worker_process) {
257            
258        if (argc < 4) {
259          fprintf(stderr, "Must give filename of image as third argument!\n");
260          exit(EXIT_FAILURE);
261        }
262        
263        bmp_filename = argv[3];
264        
265        // create empty bmp-file (black background)
266        write_blank_file(bmp_filename);
267    
268      // open file for reading and writing      // open file for reading and writing
269      hFile = CreateFile("test.bmp", GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);      hFile = CreateFile(bmp_filename, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
270      if (INVALID_HANDLE_VALUE == hFile) {      if (INVALID_HANDLE_VALUE == hFile) {
271        err = GetLastError();        err = GetLastError();
272        printErrorAndExit("Error at CreateFile",err);        printErrorAndExit("Error at CreateFile",err);
# Line 307  int main(int argc, char *argv[]) { Line 315  int main(int argc, char *argv[]) {
315            
316      // allocate memory for one worker's arguments      // allocate memory for one worker's arguments
317      if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL)      if ((worker_args = malloc(sizeof(worker_args[0]))) == NULL)
318        perror("malloc"), exit(1);        perror("Error while allocating memory for worker arguments via malloc"), exit(1);
319            
320      // assign worker's arguments      // assign worker's arguments
321      worker_args[0].start_row = worker_startrow;      worker_args[0].start_row = worker_startrow;
# Line 340  int main(int argc, char *argv[]) { Line 348  int main(int argc, char *argv[]) {
348        
349    // allocate memory for table of all worker handles    // allocate memory for table of all worker handles
350    if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL)    if ((worker_handles = malloc(workers * sizeof(worker_handles[0]))) == NULL)
351      perror("malloc"), exit(1);      perror("Error while allocating memory for worker handles via malloc"), exit(1);
352    
353    // allocate memory for table of all worker arguments    // allocate memory for table of all worker arguments
354    if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL)    if ((worker_args = malloc(workers * sizeof(worker_args[0]))) == NULL)
355      perror("malloc"), exit(1);      perror("Error while allocating memory for worker arguments via malloc"), exit(1);
356        
357        
358    // calculate bitmap segment length for workers    // calculate bitmap segment length for workers
# Line 390  int main(int argc, char *argv[]) { Line 398  int main(int argc, char *argv[]) {
398                
399      } else {      } else {
400    
401        _snprintf(szCmdline, 1023, "%s %s %i %i", argv[0], "--worker", worker_startrow, worker_rows);        if (VERBOSE)
402            verbose_option = "--verbose";
403          _snprintf(szCmdline, 1023, "%s %s %i %i %s", argv[0], "--worker", worker_startrow, worker_rows, verbose_option);
404        if (VERBOSE)        if (VERBOSE)
405          fprintf(stdout, "starting worker process: %s\n", szCmdline);          fprintf(stdout, "starting worker process: %s\n", szCmdline);
406        ZeroMemory( &si, sizeof(si) );        ZeroMemory( &si, sizeof(si) );
# Line 417  int main(int argc, char *argv[]) { Line 427  int main(int argc, char *argv[]) {
427    
428    }    }
429    
   // wait for all threads  
430    if (VERBOSE)    if (VERBOSE)
431      fprintf(stdout, "waiting for workers to finish...\n");      fprintf(stdout, "waiting for workers to finish...\n");
432    if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED)    
433      perror("WaitForMultipleObjects");    // wait for all workers
434      for (worker_index = 0; worker_index < workers; worker_index += MAXIMUM_WAIT_OBJECTS) {
435        worker_count = ((workers - worker_index) > MAXIMUM_WAIT_OBJECTS) ? MAXIMUM_WAIT_OBJECTS : (workers - worker_index);
436        if (WaitForMultipleObjects(worker_count, &worker_handles[worker_index], TRUE, INFINITE) == WAIT_FAILED)
437          printErrorAndExit("Error at WaitForMultipleObjects", GetLastError());
438      }
439    
440    // debugging: just run single thread    // debugging: just run single thread
441    //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED)    //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED)

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.16

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