/[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.5 by joko, Sat Jul 1 12:19:46 2006 UTC revision 1.7 by joko, Sat Jul 1 20:31:31 2006 UTC
# Line 94  void write_blank_file(char *filename) { Line 94  void write_blank_file(char *filename) {
94  }  }
95    
96    
97    // arguments for each thread
98    typedef struct _WorkerArguments {
99      int start_row;
100      int number_of_rows;
101      unsigned char * pBitmap;
102    } WORKERARGS, *PWORKERARGS;
103    
104    // worker thread - main entry function
105    DWORD WINAPI fractal_thread (LPVOID lpParam) {
106      
107      // thread stuff
108      int thread_id;
109      PWORKERARGS args;
110      unsigned char *pDataBitmapSegment;
111    
112      // fractal calculation
113      int x, y;
114      char bgr[3];
115    
116      thread_id = GetCurrentThreadId();
117      
118      // get worker arguments
119      args = (PWORKERARGS)lpParam;
120      
121      printf("----------------------------------------------\n");
122      printf("thread_id: %i\n", thread_id);
123      printf("arg.start_row: %i\n", args->start_row);
124      printf("arg.number_of_rows: %i\n", args->number_of_rows);
125    
126      // calculate pointer to beginning of segment
127      pDataBitmapSegment = (unsigned char *)((INT_PTR)args->pBitmap + (YSIZE - (args->start_row + args->number_of_rows)) * 3 * XSIZE);
128      printf("segment_start: %p\n", pDataBitmapSegment);
129      
130      // calculate fractal
131      for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) {
132        //printf("calc: thread=%i; y=%i            limits: %i,%i    p: %p\n", thread_id, y, args->start_row, args->number_of_rows, pDataBitmapSegment);
133        for (x = 0; x < XSIZE; x++) {
134          getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]);
135          
136          // debugging
137          //printf("pointer: %p\n", pDataBitmapCurrent);
138          
139          // transfer color values to current pixel
140          pDataBitmapSegment[0] = bgr[0];
141          pDataBitmapSegment[1] = bgr[1];
142          pDataBitmapSegment[2] = bgr[2];
143          
144          // move pointer to next pixel
145          pDataBitmapSegment += 3;
146          
147        }
148        //no padding required because 1500%4 =0
149      }
150    
151      printf("thread finished: %i\n", thread_id);
152      return 0;
153    
154    }
155    
156    
157  int main(int argc, char *argv[]) {  int main(int argc, char *argv[]) {
158    
159    // MMF support    // MMF support
# Line 103  int main(int argc, char *argv[]) { Line 163  int main(int argc, char *argv[]) {
163    unsigned char *pDataBitmap, *pDataBitmapCurrent;    unsigned char *pDataBitmap, *pDataBitmapCurrent;
164    
165    // workers    // workers
166    unsigned int workers = 3;    int workers = 10;
167    unsigned int worker_index, worker_rows, worker_startrow;    int worker_index, worker_rows, worker_startrow;
168        HANDLE *worker_handles;
169    // fractal calculation    //struct WorkerArguments *worker_args;
170    int x, y;    PWORKERARGS worker_args;
171    char bgr[3];  
     
172        
173    // create empty bmp-file (black background)    // create empty bmp-file (black background)
174    write_blank_file("test.bmp");    write_blank_file("test.bmp");
# Line 153  int main(int argc, char *argv[]) { Line 212  int main(int argc, char *argv[]) {
212    }    }
213    */    */
214    
215    // calculate segments of bitmap for worker threads/processes  
216      // allocate memory for table of all worker handles
217      if ((worker_handles = malloc(workers * sizeof worker_handles[0])) == NULL)
218        perror("malloc"), exit(1);
219    
220      /*
221      // allocate memory for table of all worker arguments
222      if ((worker_args = malloc(workers * sizeof worker_args[0])) == NULL)
223        perror("malloc"), exit(1);
224      */
225      // allocate memory for table of all worker arguments
226      worker_args = (PWORKERARGS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, workers * sizeof(WORKERARGS));
227      
228      // calculate segments of bitmap for worker threads/processes and start them
229    worker_rows = YSIZE / workers;    worker_rows = YSIZE / workers;
230    printf("rows for each worker: %i\n", worker_rows);    printf("rows for each worker: %i\n", worker_rows);
231    for (worker_index = 0; worker_index < workers; worker_index++) {    for (worker_index = 0; worker_index < workers; worker_index++) {
232            
233        // debugging: just run with single thread
234        //if (worker_index == 1)
235        //  continue;
236        
237      // number of row to start for each worker      // number of row to start for each worker
238      worker_startrow = worker_index * worker_rows;      worker_startrow = worker_index * worker_rows;
239            
# Line 166  int main(int argc, char *argv[]) { Line 242  int main(int argc, char *argv[]) {
242        worker_rows = YSIZE - worker_startrow;        worker_rows = YSIZE - worker_startrow;
243        printf("rows for last worker: %i\n", worker_rows);        printf("rows for last worker: %i\n", worker_rows);
244      }      }
245        
246        worker_args[worker_index].start_row = worker_startrow;
247        worker_args[worker_index].number_of_rows = worker_rows;
248        worker_args[worker_index].pBitmap = pDataBitmap;
249        
250        worker_handles[worker_index] = CreateThread(
251          NULL,                          // LPSECURITY_ATTRIBUTES lpThreadAttributes,
252          0,                             // SIZE_T dwStackSize,
253          fractal_thread,                // LPTHREAD_START_ROUTINE lpStartAddress,
254          &worker_args[worker_index],    // LPVOID lpParameter,
255          0,                             // DWORD dwCreationFlags,
256          NULL                           // LPDWORD lpThreadId
257        );
258        
259    }    }
260    
261    // calculate fractal    // wait for all threads
262    for (y=YSIZE-1; y>=0; y--) {    if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED)
263            for (x=0; x<XSIZE; x++) {      perror("WaitForMultipleObjects");
264              getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]);  
265                  // debugging: just run with single thread
266              // debugging    //if (WaitForSingleObject(worker_handles[0], INFINITE) == WAIT_FAILED)
267              //printf("pointer: %p\n", pDataBitmapCurrent);    //  perror("WaitForSingleObject");
268                
269              // transfer color values to current pixel    // close all worker handles
270              pDataBitmapCurrent[0] = bgr[0];    for (worker_index = 0; worker_index < workers; worker_index++)
271              pDataBitmapCurrent[1] = bgr[1];      CloseHandle(worker_handles[worker_index]);
272              pDataBitmapCurrent[2] = bgr[2];      
               
             // move pointer to next pixel  
             pDataBitmapCurrent += 3;  
               
           }  
           //no padding required because 1500%4 =0  
   }  
   
     
273    /* write the result into the file */    /* write the result into the file */
274    if (!FlushViewOfFile(pData, 0)) {    if (!FlushViewOfFile(pData, 0)) {
275      err = GetLastError();      err = GetLastError();

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.7

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