/[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.4 by joko, Sat Jul 1 11:58:49 2006 UTC revision 1.6 by joko, Sat Jul 1 13:56:54 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      unsigned int start_row;
100      unsigned 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    
111      // fractal calculation
112      int x, y;
113      char bgr[3];
114    
115      thread_id = GetCurrentThreadId();
116      
117      // get worker arguments
118      args = (PWORKERARGS)lpParam;
119      
120      printf("thread_id: %i\n", thread_id);
121      printf("arg.start_row: %i\n", args->start_row);
122      printf("arg.number_of_rows: %i\n", args->number_of_rows);
123      
124      // calculate fractal
125      for (y = (args->start_row + args->number_of_rows) - 1; y >= args->start_row; y--) {
126        for (x = 0; x < XSIZE; x++) {
127          getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]);
128          
129          // debugging
130          //printf("pointer: %p\n", pDataBitmapCurrent);
131          
132          // transfer color values to current pixel
133          args->pBitmap[0] = bgr[0];
134          args->pBitmap[1] = bgr[1];
135          args->pBitmap[2] = bgr[2];
136          
137          // move pointer to next pixel
138          args->pBitmap += 3;
139          
140        }
141        //no padding required because 1500%4 =0
142      }
143    
144      return 0;
145    
146    }
147    
148    
149  int main(int argc, char *argv[]) {  int main(int argc, char *argv[]) {
150    
151    // MMF support    // MMF support
# Line 102  int main(int argc, char *argv[]) { Line 154  int main(int argc, char *argv[]) {
154    LPVOID pData;    LPVOID pData;
155    unsigned char *pDataBitmap, *pDataBitmapCurrent;    unsigned char *pDataBitmap, *pDataBitmapCurrent;
156    
157    // fractal calculation    // workers
158    int x, y;    unsigned int workers = 3;
159    char bgr[3];    unsigned int worker_index, worker_rows, worker_startrow;
160        HANDLE *worker_handles;
161      //struct WorkerArguments *worker_args;
162      PWORKERARGS worker_args;
163    
164        
165    // create empty bmp-file (black background)    // create empty bmp-file (black background)
166    write_blank_file("test.bmp");    write_blank_file("test.bmp");
# Line 149  int main(int argc, char *argv[]) { Line 204  int main(int argc, char *argv[]) {
204    }    }
205    */    */
206    
207    // calculate fractal  
208    for (y=YSIZE-1; y>=0; y--) {    // allocate memory for table of all worker handles
209            for (x=0; x<XSIZE; x++) {    if ((worker_handles = malloc(workers * sizeof worker_handles[0])) == NULL)
210              getColorValuesAt(x * (2.0 / XSIZE) - 1.5, y * (2.0 / YSIZE) - 1.0, &bgr[2], &bgr[1], &bgr[0]);      perror("malloc"), exit(1);
211                
212              // debugging    /*
213              //printf("pointer: %p\n", pDataBitmapCurrent);    // allocate memory for table of all worker arguments
214                  if ((worker_args = malloc(workers * sizeof worker_args[0])) == NULL)
215              // transfer color values to current pixel      perror("malloc"), exit(1);
216              pDataBitmapCurrent[0] = bgr[0];    */
217              pDataBitmapCurrent[1] = bgr[1];    // allocate memory for table of all worker arguments
218              pDataBitmapCurrent[2] = bgr[2];    worker_args = (PWORKERARGS) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, workers * sizeof(WORKERARGS));
219                  
220              // move pointer to next pixel    // calculate segments of bitmap for worker threads/processes and start them
221              pDataBitmapCurrent += 3;    worker_rows = YSIZE / workers;
222                  printf("rows for each worker: %i\n", worker_rows);
223            }    for (worker_index = 0; worker_index < workers; worker_index++) {
224            //no padding required because 1500%4 =0      
225        // number of row to start for each worker
226        worker_startrow = worker_index * worker_rows;
227        
228        // recalculate number of rows for last worker if (YSIZE mod workers) != 0
229        if (worker_index == workers - 1) {
230          worker_rows = YSIZE - worker_startrow;
231          printf("rows for last worker: %i\n", worker_rows);
232        }
233        
234        worker_args[worker_index].start_row = worker_startrow;
235        worker_args[worker_index].number_of_rows = worker_rows;
236        worker_args[worker_index].pBitmap = pDataBitmap;
237        
238        worker_handles[worker_index] = CreateThread(
239          NULL,                          // LPSECURITY_ATTRIBUTES lpThreadAttributes,
240          0,                             // SIZE_T dwStackSize,
241          fractal_thread,                // LPTHREAD_START_ROUTINE lpStartAddress,
242          &worker_args[worker_index],    // LPVOID lpParameter,
243          0,                             // DWORD dwCreationFlags,
244          NULL                           // LPDWORD lpThreadId
245        );
246        
247    }    }
248    
249      // wait for all threads
250      if (WaitForMultipleObjects(workers, worker_handles, TRUE, INFINITE) == WAIT_FAILED)
251        perror("WaitForMultipleObjects");
252        
253    /* write the result into the file */    /* write the result into the file */
254    if (!FlushViewOfFile(pData, 0)) {    if (!FlushViewOfFile(pData, 0)) {

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

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