c - MPI、调用和释放 :

标签 c free mpi calloc

我正在尝试学习 C 和 MPI,这个程序计算 n 个 float 的总和。但是我有一个错误:

/home/xx/PRIMO/primo.exe: free(): 下一个大小无效(快):0x000000000109bda0 /home/xx/PRIMO/primo.exe: free(): 下一个尺寸无效(快):0x00000000024fada0

有 2 天我不知道该转向哪条路。这里的程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mpi.h"

#define HOST 0
// prototypes
void initialize( int argc, char *argv[] );
float *create_array( int n );
void data_scatter( float *numbers, float *numbers_loc, int packet_size, int rest );
void validations();

// global vars
int menum, nproc, namelen;

int main(int argc, char *argv[]) {

  initialize( argc, argv );
  if( menum == HOST ) printf("Program started\n");
  if( menum == HOST ) validations(); 

  // ****** Declaring variebles ******
  int n, tag, packet_size, rest, j, i;
  float *numbers, *numbers_loc;
  float sum = 0.0;
  numbers_loc = (float*) calloc( packet_size, sizeof (float) );
  MPI_Status info;

  // ****** data distribution ******
  scanf("%d", &n);
  if( menum == HOST ) numbers = create_array( n );  

  MPI_Bcast( &n, 1, MPI_INT, 0, MPI_COMM_WORLD );
  double start_time = MPI_Wtime();

  packet_size = n / nproc;
  rest = n % nproc;
  if( menum < rest ) ++packet_size;

  data_scatter( numbers, numbers_loc, packet_size, rest );

  // ****** partial sums, first level ******
  for( j = 0; j < packet_size; j++) {
    sum += numbers_loc[j];
  } 
  free(numbers_loc);

  // ****** iterating phase ******
  for( i = 0; i < log2(nproc); i++) {

    int pow1 = (int) pow(2, i);
    tag = pow1 + 15; 

    if( (menum % pow1) == 0) {

      int pow2 = (int) pow(2,i+1);
      if( (menum % pow2) == 0 ) {

        float other_sum = 0;
        MPI_Recv( &other_sum, 1, MPI_INT, menum + pow1, tag, MPI_COMM_WORLD, &info );
        sum += other_sum;

      } else {

        MPI_Send( &sum, 1, MPI_INT, menum - pow1, tag, MPI_COMM_WORLD );

      }
    }
  }  

  // ****** printing result ******
  if( menum == HOST ) {
    printf( "TOTAL SUM: %.2f\n", sum);
  }

  double end_time = MPI_Wtime();
  double time = end_time - start_time;
  double max = 0;

  printf( "%d time %f\n", menum, time );
  MPI_Reduce( &time, &max, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD );
  if(menum == HOST) printf( "%d max time %f\n", menum, max );

  printf("Program terminated\n");
  MPI_Finalize();
  return 0;
}

void initialize( int argc, char *argv[] ) {

  MPI_Init(&argc,&argv);
  MPI_Comm_rank(MPI_COMM_WORLD,&menum);
  MPI_Comm_size(MPI_COMM_WORLD,&nproc);

}


float *create_array( int n ) {

    float sequenzial_sum = 0;
    int i;

    printf("Creating Array of dimension: %d\n", n);

    float *array = (float*) calloc( n, sizeof(float));
    srand( time(NULL) );

    for( i=0; i < n; i++) {

      array[i] = ((float) rand())/ ((float) RAND_MAX);
      sequenzial_sum += array[i]; 

      printf("-%f-", array[i]);
    }

    printf("\n");
    printf("Array creation terminated, sum = %.2f \n", sequenzial_sum);

    return array;
}

void data_scatter( float *numbers, float *numbers_loc, int packet_size, int rest ) {
  MPI_Status info;
  int start = 0, offset = packet_size, i, tag;

  if( menum == HOST ) {

    memcpy(numbers_loc, numbers, packet_size * sizeof(float));
    for( i=1; i < nproc; i++ ) {

      start += offset; 
      if( rest == i) --offset;

      tag = 22 + i;
      MPI_Send( &numbers[start], offset, MPI_INT, i, tag, MPI_COMM_WORLD );

    }

    free(numbers);

  } else {

    tag = 22 + menum;
    MPI_Recv( numbers_loc, packet_size, MPI_INT, 0, tag, MPI_COMM_WORLD, &info );

  }

}

void validations() {

  if( ( fmod(log2( nproc ),  1.0) != 0 )) {

    printf("CPU number must ne a power of 2\n");
    MPI_Abort( MPI_COMM_WORLD, -1 );

  }

}

最佳答案

int n, tag, packet_size, rest, j, i;
float *numbers, *numbers_loc;
float sum = 0.0;
numbers_loc = (float*) calloc( packet_size, sizeof (float) );

我不确定我是否遗漏了什么,但是 packet_size在这里没有被初始化就被使用了。用 -Wall 编译实际上会警告您这一点。

-Wall还会警告您缺少 <time.h>头文件。

关于c - MPI、调用和释放 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10269960/

相关文章:

c - 我如何在 linux 内核中不使用 malloc() 创建缓冲区?

c - 如何在 main 中返回指针到指针的指针?

c - 释放分配的字符串时内存损坏

python - mpi4py | comm.bcast 不起作用

c - 当函数在不同文件中实现时 ((always_inline)) 不起作用

c - 在字符串中查找动词

c - 如何从 Windows 文件过滤器驱动程序中的路径中删除文件名?

c - C中指针、变量和值的内存位置?

python - SLURM srun 不并行运行 python 脚本,但可以访问并行资源

fortran - 构建MPI程序时出错