2012-03-22 12 views
1

私はエラーが発生しました:割り当てに無効な左辺値があります。pthreadを使用したマンデルブロ画像の作成

私のプログラムではこれが唯一のエラーです。特にpthreadに関しては致命的なコンパイル時エラーと思われます。

コマンドライン引数を使用してランタイムで入力を取得しようとしています。そのため、プログラムで初期化された静的入力でプログラムを実行するとエラーは発生しませんでした自体。ここ

#include <stdlib.h> 
#include <stdio.h> 
#include <pthread.h> 
#include <time.h> 
#include <sched.h> 
#include <sys/types.h> 
#include <math.h> 
#include <time.h> 
#include <sys/time.h> 
#include <stdint.h> 
#define num_threads 8 
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; 
unsigned int width = 1500; 
unsigned int height = 1500; 
unsigned int max_iterations = 30000; 
unsigned int **color = NULL; 
double min_re; 
double max_re; 
double min_im; 
double max_im; 
double x_factor; 
double y_factor; 
unsigned int NUM_OF_THREADS; 
int chunk = 10; 
int total_sum = 0; 

bool file_write() 
{ 
FILE *fractal = fopen("mandelbrot_imagepthread.ppm","w+"); 
if(fractal != NULL) 
{ 
fprintf(fractal,"P6\n"); 
    fprintf(fractal,"# %s\n", "Mandelbrot_imagepthread.ppm"); 
    fprintf(fractal,"%d %d\n", height, width); 
    fprintf(fractal,"255\n"); 

int y = 0, x = 0; 
unsigned int R = 0, G = 0, B = 0; 
for(x = 0; x < width; ++x) 
{ 
    for(y = 0; y < height; ++y) 
    { 
     R = (color[y][x]*10)%255; 
      G = 255-((color[y][x]*10)%255); 
      B = ((color[y][x]*10)-150)%255; 
     if(R == 10) R = 11; 
     if(G == 10) G = 11; 
     if(B == 10) B = 11; 
      putc(R, fractal); 
      putc(G, fractal); 
      putc(B, fractal); 
    } 
} 
fclose(fractal); 
} 
return true; 
} 
int method(int x, int y, int max_iterations, double max_im,double min_re,double x_factor, double y_factor) 
{ 
    double c_im = max_im - y*y_factor; 
    double c_re = min_re + x*x_factor; 
    double Z_re = c_re, Z_im = c_im; 
    unsigned int col = 0; 
    for(unsigned n=0; n<max_iterations; ++n) 
     { 
      double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im; 
      if(Z_re2 + Z_im2 > 4) 
       { 
        col = n; 
        break; 
       } 
        Z_im = 2 * Z_re * Z_im + c_im; 
        Z_re = Z_re2 - Z_im2 + c_re; 
     } 
    return col; 
} 
void* method1(void* t) 
{ 
double min_re = -2.0; 
double max_re = 1.0; 
double min_im = -1.2; 
double max_im = min_im+(max_re-min_re)*height/width; 
double x_factor = (max_re-min_re)/(width-1); 
double y_factor = (max_im-min_im)/(height-1); 
int x,y; 
int sub_total = -1; 
pthread_mutex_lock(&mut); 
if(total_sum < height) 
{ 
    sub_total = total_sum; 
    total_sum = total_sum + chunk; 
} 
pthread_mutex_unlock(&mut); 
while(sub_total > -1) 
{ 
    int start_point = sub_total; 
    int end_point = start_point + chunk; 
    for(y=start_point; y<end_point; y++) 
    { 
     for(x=0; x<width; ++x) 
     { 
      int m1; 
      uintptr_t m2; 
      m2 = (uintptr_t)t; 
      m1 = method(x,y,max_iterations,max_im,min_re,x_factor,y_factor); 
      if(m1) 
      { 
       color[x][y] = m1*40; 
      } 
     } 
    } 
    sub_total = -1; 
    pthread_mutex_lock(&mut); 
    if(total_sum < height) 
    { 
     sub_total = total_sum; 
     total_sum = total_sum + chunk; 
    } 
    pthread_mutex_unlock(&mut); 
} 
    pthread_exit((void*)&t); 
} 
int main(int argc, char *argv[]) 
{ 
if(argc != 9) 
{ 
printf("There is an error in the input given.\n"); 
return 0; 
} 
else 
{ 
    height = atoi(argv[1]); 
    width = atoi(argv[2]); 
max_iterations = atoi(argv[3]); 
min_re = atof(argv[4]); 
max_re = atof(argv[5]); 
min_im = atof(argv[6]); 
max_im = atof(argv[7]); 
num_threads = atoi(argv[8]); 
} 
color = (unsigned int**)malloc(height*sizeof(unsigned int*)); 
x_factor = (max_re-min_re)/(width-1); 
y_factor = (max_im-min_im)/(height-1); 
printf("height = %d\twidth = %d\tmaximum_iterations = %d\tminimum_x-value = %.2f\tmaximum_x-value = %.2f\tminimum_y-value = %.2f\tmaximum_y-value = %.2f\tno. of threads = %d\t\n",height,width,max_iterations,min_re,max_re,min_im,max_im,num_threads); 
int x; 
for(x = 0; x < height; x++) 
{ 
color[x] = (unsigned int*)malloc(width*sizeof(unsigned int)); 
} 
time_t ts,te; 
time(&ts); 
pthread_t t1[num_threads]; 
pthread_attr_t attr; 
int l1; 
void *att; 
double value = 0.0; 
pthread_attr_init(&attr); 
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); 
for(int i=0;i<num_threads;i++) 
{ 
    l1 = pthread_create(&t1[i], &attr, method1, (void *) i); 
    if(l1) 
    { 
     printf("There is some kind of error in thread creation: %d", l1); 
     exit(-1); 
    } 
} 
pthread_attr_destroy(&attr); 
for(int i=0;i<num_threads;i++) 
{ 
    l1 = pthread_join(t1[i],&att); 
    if(l1) 
    { 
     printf("There is some kind of error in thread creation: %d", l1); 
     exit(-1); 
    } 
    double result = *(double *)att; 
    value += result; 
} 
time(&te); 
double diff = difftime(te,ts); 
file_write(); 
printf("Total Time elapsed: %.2f seconds\n",diff); 
for(x = 0; x < height; x++) 
{ 
    free(color[x]); 
} 
free(color); 
return 0; 
pthread_exit(NULL); 

}

答えて

2

エラーがあなたの代わりにint型として宣言の#defineディレクティブで8すべきNUM_THREADSを定義することです! #define num_threads 8からint num_threads=8;

一般的には、#defineディレクティブは邪悪ではないので避けてください。 グローバル定数変数に#defineではなくstatic constと宣言したい場合は、これらのディレクティブは、プリプロセッサによって次のコードに置き換えられ、次の(非センス)コードにつながります。

+0

int num_threads = 8; –

+1

権利:Pはタイプミスのために私を許します。 – linello

+0

その動作は完全に@linello –

関連する問題