我尝试从文件中读取浮点数,然后对它们进行排序。排序必须是并行 UPC。这是当前代码:
#include <upc_relaxed.h>
#include <upc_collective.h>
#include <stdio.h>
#include <stdlib.h>
int lt_int( shared void *x, shared void *y ) {
int x_val = *(shared int *)x,
y_val = *(shared int *)y;
return x_val > y_val ? -1 : x_val < y_val ? 1 : 0;
}
shared int size=0;
int main(int argc, char* argv[]) {
FILE *f;
int i=0;
if (MYTHREAD == 0) {
f = fopen ("dane.dat", "r");
while (feof(f) == 0) {
fscanf (f, "%f\n");
++size;
}
fclose(f);
}
upc_barrier;
/* allocation goes wrong! */
shared [] float *array = upc_all_alloc(size, sizeof(float));
/* printf("%d\n",sizeof(array)); // it returns 8! */
upc_barrier;
if (MYTHREAD == 0) {
f = fopen ("dane.dat", "r");
i=0;
while (feof(f) == 0) {
printf("%d\n", i);
/* segmentation fault! */
fscanf (f, "%f\n", &array[i]);
printf("%f\n", array[i]);
i++;
}
fclose(f);
}
upc_barrier;
upc_all_sort( array, sizeof(float), size/THREADS, size, lt_int, UPC_IN_ALLSYNC);
upc_barrier;
if (MYTHREAD == 0) {
for (i = 0; i<=atoi(argv[1]) ; ++i) {
printf("%f\n", array[atoi(argv[1]) + (size/atoi(argv[1]))]);
}
}
return 0;
}
而且我不知道我做错了什么。我得到分段错误,因为内存分配出错。你能帮助我吗?