MPI_Probe-Dynamically allocate memory

在MPI并行编程中,当接收方不确定消息大小时,可以使用MPI_Probe先探测消息尺寸,然后根据探测到的尺寸动态分配内存,再进行接收。通过这种方法,可以避免预定义过大内存空间。例如,进程1向其他进程发送不同数量的数据,其他进程通过MPI_Probe获取实际数据量,然后接收。输出显示了各个进程接收的数据数量。

某些情况下,接收者不知道消息尺寸,此时当然可以用一个足够大的空间去接收,但我们可以用MPI_Probe去首先探测消息尺寸,然后再分配相应空间,然后再正式接收消息,可以认为MPI_Probe和MPI_Recv很类似,只是不真正执行接收操作

MPI_Probe(
int source, //source rank, or MPI_ANY_SOURCE (integer)
int tag, //tag value or MPI_ANY_TAG (integer)
MPI_Comm comm, //communicator (handle)
MPI_Status *status)

例子,设进程1随机决定发送给其它进程的数据个数,其他进程需要先探测真实尺寸,然后接收:

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
int main(int argc, char** argv) {
  MPI_Init(NULL, NULL);

  int nProcs,Rank;
  int i;
  MPI_Status status;
  MPI_Comm_size(MPI_COMM_WORLD, &nProcs);
  MPI_Comm_rank(MPI_COMM_WORLD, &Rank);

  int NumToBeSent;

  if (Rank == 0) {

    const int MaxNum = 6000;
    std::vector<double> Data;
    srand(time(NULL));

        for(i=0;i<MaxNum;i++)Data.resize(MaxNum);
        for(i=0;i<MaxNum;i++)Data[i] = i;

    for(i=1;i<nProcs;i++)
    {   
        NumToBeSent = rand() % MaxNum;
        MPI_Send(&Data[0],NumToBeSent, MPI_DOUBLE, i, 0, MPI_COMM_WORLD);
        printf("0 sent %d numbers to %d\n", NumToBeSent,i);
    }
  }
  else {

     MPI_Probe(0, 0, MPI_COMM_WORLD, &status);
    //When MPI_Probe returns, the size information is included, the MPI_Get_count function can extract size information
     MPI_Get_count(&status, MPI_DOUBLE, &NumToBeSent);
      // Now we can allocate suitable memory for the receive buffer
    double* recvbuf = (double*)malloc(sizeof(double) * NumToBeSent);
    // Now the message can be received properly
    MPI_Recv(recvbuf, NumToBeSent, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD,&status);
    printf("%d Received %d Data From 0.\n", Rank, NumToBeSent);
    free(recvbuf);
  }
  system("pause");
  MPI_Finalize();
}

一个执行输出如下(设4个进程):
0 sent 5646 numbers to 1
0 sent 430 numbers to 2
0 sent 1365 numbers to 3

1 received 5646 Data From 0
2 received 430 Data From 0
3 received 1365 Data From 0

arm-v01c02-linux-musleabi-gcc -mfloat-abi=softfp -mfpu=neon-vfpv4 -fPIE -pie -I./common -L./lib -DOT_ACODEC_TYPE_INNER -DOT_VQE_USE_STATIC_MODULE_REGISTER -o sample_audio sample_audio.c -laac_comm -lbnr -lir_auto -lprotobuf-c -lsns_sc450ai -lss_mpi_isp -lvqe_agc -lvqe_talkv2 -laac_dec -lcalcflicker -lldci -lsecurec -lsns_sc500ai -lss_mpi_audio -lss_mpi_ive -lss_mpi_uvc -lvqe_anr -laac_enc -ldehaze -lmbedtls_harden_adapt -lsns_gc4023 -lss_bcd -lss_mpi_audio_adp -lvqe_eq -laac_sbr_dec -ldnvqe -lmp3_dec -lsns_os04d10 -lss_ivs_md -lss_mpi_awb -lss_mpi_otp -lupvqe -lvqe_hpf -laac_sbr_enc -ldrc -lopus -lsns_sc431hai -lss_mpi_cipher -lss_mpi_smartae -lvoice_engine -lvqe_record -lacs -lextend_stats -lot_mpi_isp -lsns_sc4336p -lss_mpi_ae -lss_mpi_devstat -lss_mpi_sysbind -lvqe_aec -lvqe_res -lstdc++ -lpthread -lm -ldl /opt/linux/x86-arm/arm-v01c02-linux-musleabi-gcc/bin/../lib/gcc/arm-linux-musleabi/10.3.0/../../../../arm-linux-musleabi/bin/ld: /tmp/cciWniZq.o: in function `sample_audio_exit_proc': sample_audio.c:(.text+0xa6c): undefined reference to `sample_comm_audio_destroy_all_thread' /opt/linux/x86-arm/arm-v01c02-linux-musleabi-gcc/bin/../lib/gcc/arm-linux-musleabi/10.3.0/../../../../arm-linux-musleabi/bin/ld: sample_audio.c:(.text+0xa70): undefined reference to `sample_comm_audio_exit' /opt/linux/x86-arm/arm-v01c02-linux-musleabi-gcc/bin/../lib/gcc/arm-linux-musleabi/10.3.0/../../../../arm-linux-musleabi/bin/ld: /tmp/cciWniZq.o: in function `sample_audio_adec_ao_inner': sample_audio.c:(.text+0x10d6): undefined reference to `sample_comm_audio_ao_bind_adec' /opt/linux/x86-arm/arm-v01c02-linux-musleabi-gcc/bin/../lib/gcc/arm-linux-musleabi/10.3.0/../../../../arm-linux-musleabi/bin/ld: sample_audio.c:(.text+0x112c): undefined reference to `sample_comm_audio_create_thread_file_adec'
最新发布
11-11
当使用 `arm-v01c02-linux-musleabi-gcc` 编译 `sample_audio.c` 出现 `undefined reference to 'sample_comm_audio_destroy_all_thread'` 等错误,表明函数的调用方无法获取到函数的定义信息,对于一个模块而言,极有可能是因为被调用函数没有被成功的包含[^3]。以下是一些可能的解决办法: ### 检查源文件和头文件 确保包含了定义 `sample_comm_audio_destroy_all_thread` 函数的源文件和头文件。如果该函数定义在 `sample_comm_audio.c` 文件中,需要在编译命令中添加该文件: ```bash arm-v01c02-linux-musleabi-gcc -mfloat-abi=softfp -mfpu=neon-vfpv4 -fPIE -pie -I./common -DOT_ACODEC_TYPE_INNER -DOT_VQE_USE_STATIC_MODULE_REGISTER -o sample_audio sample_audio.c sample_comm_audio.c ./lib/libaac_comm.so ... ``` 同时,在 `sample_audio.c` 文件中包含对应的头文件,例如: ```c #include "sample_comm_audio.h" ``` ### 检查库文件 如果 `sample_comm_audio_destroy_all_thread` 函数是在某个库文件中定义的,要确保该库文件被正确链接。可以使用 `-L` 选项指定库文件的搜索路径,使用 `-l` 选项指定库名。例如,如果该函数在 `libsample_comm_audio.so` 库中,编译命令可以修改为: ```bash arm-v01c02-linux-musleabi-gcc -mfloat-abi=softfp -mfpu=neon-vfpv4 -fPIE -pie -I./common -DOT_ACODEC_TYPE_INNER -DOT_VQE_USE_STATIC_MODULE_REGISTER -o sample_audio sample_audio.c -L/path/to/library -lsample_comm_audio ./lib/libaac_comm.so ... ``` ### 检查库文件路径 如果库文件不在默认的搜索路径中,除了使用 `-L` 选项,还可以设置 `LD_LIBRARY_PATH` 环境变量。临时设置可以在终端中执行以下命令: ```bash export LD_LIBRARY_PATH=/path/to/library:$LD_LIBRARY_PATH ``` 如果想永久设置,可以将上述命令添加到 `~/.bashrc` 或 `/etc/profile` 文件中,然后执行 `source ~/.bashrc` 或 `source /etc/profile` 使设置生效。 ### 检查函数声明和定义 确保 `sample_comm_audio_destroy_all_thread` 函数的声明和定义一致。函数的参数类型、返回值类型等必须完全匹配。 ### 检查库文件是否损坏 可以使用 `file` 命令检查库文件是否损坏。例如: ```bash file /path/to/library/libsample_comm_audio.so ``` 如果库文件损坏,需要重新获取或编译该库文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值