我收到以下类型的错误。它适用于1个线程,但是如果我在2个或更多线程上启动此程序,则会失败。我无法弄清楚出了什么问题。
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#define ind(i, j) (((i + l->nx) % l->nx) + ((j + l->ny) % l->ny) * (l->nx))
typedef struct {
int nx, ny;
int *u0;
int *u1;
int steps;
int save_steps;
} life_t;
void life_init(const char *path, life_t *l);
void life_free(life_t *l);
void life_step(life_t *l);
//void life_save_vtk(const char *path, life_t *l);
int main(int argc, char **argv)
{
if (argc != 2) {
printf("Usage: %s input file.\n", argv[0]);
return 0;
}
life_t l;
//clock_t t;
//t = clock();
life_init(argv[1], &l);
int i;
char buf[100];
for (i = 0; i < l.steps; i++) {
/*if (i % l.save_steps == 0) {
sprintf(buf, "life_%06d.vtk", i);
printf("Saving step %d to '%s'.\n", i, buf);
life_save_vtk(buf, &l);
}*/
life_step(&l);
}
life_free(&l);
//t = clock() - t;
//printf ("It took me %d clicks (%f seconds).\n", (int)t, ((double)t)/CLOCKS_PER_SEC);
return 0;
}
void life_init(const char *path, life_t *l)
{
FILE *fd = fopen(path, "r");
assert(fd);
assert(fscanf(fd, "%d\n", &l->steps));
assert(fscanf(fd, "%d\n", &l->save_steps));
printf("Steps %d, save every %d step.\n", l->steps, l->save_steps);
assert(fscanf(fd, "%d %d\n", &l->nx, &l->ny));
printf("Field size: %dx%d\n", l->nx, l->ny);
l->u0 = (int*)calloc(l->nx * l->ny, sizeof(int));
l->u1 = (int*)calloc(l->nx * l->ny, sizeof(int));
int i, j, r, cnt;
cnt = 0;
while ((r = fscanf(fd, "%d %d\n", &i, &j)) != EOF) {
l->u0[ind(i, j)] = 1;
cnt++;
}
printf("Loaded %d life cells.\n", cnt);
fclose(fd);
}
void life_free(life_t *l)
{
free(l->u0);
free(l->u1);
l->nx = l->ny = 0;
}
void life_step(life_t *l)
{
int i, j;
#pragma omp parallel private(i, j)
{
int rank = omp_get_thread_num();
int size = omp_get_num_threads();
int starty = (rank)*l->ny/size;
int endy = (rank+1)*l->ny/size;
int startx = (rank)*l->nx/size;
int endx = (rank+1)*l->nx/size;
for (j = starty; j < endy; j++) {
for (i = startx; i < endx; i++) {
int n = 0;
n += l->u0[ind(i+1, j)];
n += l->u0[ind(i+1, j+1)];
n += l->u0[ind(i, j+1)];
n += l->u0[ind(i-1, j)];
n += l->u0[ind(i-1, j-1)];
n += l->u0[ind(i, j-1)];
n += l->u0[ind(i-1, j+1)];
n += l->u0[ind(i+1, j-1)];
l->u1[ind(i,j)] = 0;
if (n == 3 && l->u0[ind(i,j)] == 0) {
l->u1[ind(i,j)] = 1;
}
if ((n == 3 || n == 2) && l->u0[ind(i,j)] == 1) {
l->u1[ind(i,j)] = 1;
}
}
}
int *tmp;
tmp = l->u0;
#pragma omp barrier
l->u0 = l->u1;
#pragma omp master
l->u1 = tmp;
}
}
我得到了这个错误:
*** Error in `./a.out': double free or corruption (!prev): 0x0000000001760270 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7d053)[0x7f271d265053]
./a.out[0x400ca2]
./a.out[0x400a2e]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f271d209b15]
./a.out[0x4008d9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 b6:33fa1 1967354 /home/mpi201617/openmp/a.out
00601000-00602000 r--p 00001000 b6:33fa1 1967354 /home/mpi201617/openmp/a.out
00602000-00603000 rw-p 00002000 b6:33fa1 1967354 /home/mpi201617/openmp/a.out
01760000-01781000 rw-p 00000000 00:00 0 [heap]
7f2718000000-7f2718021000 rw-p 00000000 00:00 0
7f2718021000-7f271c000000 ---p 00000000 00:00 0
7f271c5c0000-7f271c5d5000 r-xp 00000000 b6:33fa1 524309 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f271c5d5000-7f271c7d4000 ---p 00015000 b6:33fa1 524309 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f271c7d4000-7f271c7d5000 r--p 00014000 b6:33fa1 524309 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f271c7d5000-7f271c7d6000 rw-p 00015000 b6:33fa1 524309 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f271c7e6000-7f271c7e7000 rw-p 00000000 00:00 0
7f271c7e7000-7f271c7e8000 ---p 00000000 00:00 0
7f271c7e8000-7f271d1e8000 rw-p 00000000 00:00 0
7f271d1e8000-7f271d39f000 r-xp 00000000 b6:33fa1 526414 /usr/lib64/libc-2.17.so
7f271d39f000-7f271d59f000 ---p 001b7000 b6:33fa1 526414 /usr/lib64/libc-2.17.so
7f271d59f000-7f271d5a3000 r--p 001b7000 b6:33fa1 526414 /usr/lib64/libc-2.17.so
7f271d5a3000-7f271d5a5000 rw-p 001bb000 b6:33fa1 526414 /usr/lib64/libc-2.17.so
7f271d5a5000-7f271d5aa000 rw-p 00000000 00:00 0
7f271d5b0000-7f271d5c6000 r-xp 00000000 b6:33fa1 526440 /usr/lib64/libpthread-2.17.so
7f271d5c6000-7f271d7c6000 ---p 00016000 b6:33fa1 526440 /usr/lib64/libpthread-2.17.so
7f271d7c6000-7f271d7c7000 r--p 00016000 b6:33fa1 526440 /usr/lib64/libpthread-2.17.so
7f271d7c7000-7f271d7c8000 rw-p 00017000 b6:33fa1 526440 /usr/lib64/libpthread-2.17.so
7f271d7c8000-7f271d7cc000 rw-p 00000000 00:00 0
7f271d7d0000-7f271d7e6000 r-xp 00000000 b6:33fa1 529119 /usr/lib64/libgomp.so.1.0.0
7f271d7e6000-7f271d9e5000 ---p 00016000 b6:33fa1 529119 /usr/lib64/libgomp.so.1.0.0
7f271d9e5000-7f271d9e6000 r--p 00015000 b6:33fa1 529119 /usr/lib64/libgomp.so.1.0.0
7f271d9e6000-7f271d9e7000 rw-p 00016000 b6:33fa1 529119 /usr/lib64/libgomp.so.1.0.0
7f271d9e7000-7f271d9e8000 rw-p 00000000 00:00 0
7f271d9e8000-7f271da09000 r-xp 00000000 b6:33fa1 529085 /usr/lib64/ld-2.17.so
7f271dbfe000-7f271dc00000 rw-p 00000000 00:00 0
7f271dc07000-7f271dc08000 rw-p 00000000 00:00 0
7f271dc09000-7f271dc0a000 r--p 00021000 b6:33fa1 529085 /usr/lib64/ld-2.17.so
7f271dc0a000-7f271dc0b000 rw-p 00022000 b6:33fa1 529085 /usr/lib64/ld-2.17.so
7f271dc0b000-7f271dc0d000 rw-p 00000000 00:00 0
7f271dc0d000-7f271dc0e000 rw-p 00000000 00:00 0
7ffd7be92000-7ffd7bea7000 rw-p 00000000 00:00 0 [stack]
7ffd7bfa8000-7ffd7bfaa000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]