gpt4 book ai didi

c++ - 使用 pthread 创建 mandelbrot 图像

转载 作者:行者123 更新时间:2023-11-30 04:28:49 26 4
gpt4 key购买 nike

我收到一个错误:赋值中的左值无效。

这是我的程序唯一的错误,它看起来像是一个致命的编译时错误,特别是 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);

最佳答案

此处的错误是您使用#define 指令将num_threads 定义为8,而不是将其声明为int!将 #define num_threads 8 更改为 int num_threads=8;

一般来说,你应该避免使用#define 指令,因为它们是邪恶的。如果你想让一个全局常量变量声明为 static const 而不是 #define。这些指令由预处理器替换为以下代码,并导致以下(无意义)代码。

8 = atoi(argv[8])

关于c++ - 使用 pthread 创建 mandelbrot 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9822864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com