首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UPC分配动态数组和排序

UPC分配动态数组和排序
EN

Stack Overflow用户
提问于 2012-01-27 08:02:59
回答 1查看 370关注 0票数 0

我尝试从文件中读取浮点数,然后对它们进行排序。排序必须是paralell UPC。这是当前的代码:

代码语言:javascript
复制
#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;
}

我不知道我做错了什么。我得到了分段错误,因为内存分配出错。你能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2012-01-27 08:28:59

这个调用是错误的:

代码语言:javascript
复制
fscanf (f, "%f\n");

此外,您的对象array是指向float的指针。这是正常的,sizeof array将返回指针类型的大小(在您的实现中为8),而不是您分配的数组对象的大小。您应该检查upc_all_alloc的返回值,以验证分配过程中没有错误(如果返回值为== NULL,则表示分配失败)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9027323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档