I try to read floats from a file and then sort them. Sort must be paralell UPC. That's current code:
#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;
}
And I don't know what am I doing wrong. I get segmentation fault, becouse allocation of memory goes wrong. Can you help me?
This call is wrong:
Also your object
array
is a pointer tofloat
. It is normalsizeof array
will return the size of the pointer type (8 in your implementation) and not the size of the array object you allocated. You should check the return value ofupc_all_alloc
to verify there was no error during allocation (if the return value== NULL
, the allocation failed).