fox 算法 mpi 实现

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<math.h>
#include <string.h>
#include "mpi.h"
#define matrixLength 8
#define matrixMaxValue 50


void initialMatrix(int matrixA[matrixLength][matrixLength],int matrixB[matrixLength][matrixLength])
    {int i=-1;
     int j=-1;
    for(i=0;i<matrixLength;i++)
        for(j=0;j<matrixLength;j++)
            {matrixA[i][j]=i*matrixLength+j;
             matrixB[i][j]=1;            

            }

    }
int main(int argc, char  *argv[])
    {
    /*serial implementation*/

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

    const int childMatrixLength=matrixLength/(int)pow(nproc,0.5);

    int A[childMatrixLength][childMatrixLength];//every process has a small matrix
    int B[childMatrixLength][childMatrixLength];
    int C[childMatrixLength][childMatrixLength];

    int tempA[matrixLength*matrixLength];
    int tempB[matrixLength*matrixLength];
    int i=-1;
    int j=-1;
    memset(C,0,matrixLength*matrixLength/nproc*sizeof(int));


        srand(time(0));
        if(0==rank)
        {int matrixA[matrixLength][matrixLength];
         int matrixB[matrixLength][matrixLength];
         initialMatrix(matrixA,matrixB);

         int tempCount=0;
         int i=-1;
         int j=-1;
         for(i=0;i<pow(nproc,0.5);i++)
            for(j=0;j<pow(nproc,0.5);j++)
                {int row=-1;
                 int col=-1;
                 for(row=i*childMatrixLength;row<(i+1)*childMatrixLength;row++)
                    for(col=j*childMatrixLength;col<(j+1)*childMatrixLength;col++)
                        {
                         tempA[tempCount]=matrixA[row][col];
                         tempB[tempCount]=matrixB[row][col];
                         tempCount++;
                        }

                }
        for(i=0;i<matrixLength*matrixLength;i++)
            printf("%d ",tempA[i]); 
        printf("\n");



        }
        MPI_Scatter(tempA,childMatrixLength*childMatrixLength,MPI_INT,A,childMatrixLength*childMatrixLength,MPI_INT,0,MPI_COMM_WORLD);
        MPI_Scatter(tempB,childMatrixLength*childMatrixLength,MPI_INT,B,childMatrixLength*childMatrixLength,MPI_INT,0,MPI_COMM_WORLD);



        /*parallel implementation  fox*/

        int messageCount=0;
        int receiveDataRow[childMatrixLength][childMatrixLength];
        int receiveDataCol[childMatrixLength][childMatrixLength];
        for(i=0;i<childMatrixLength;i++)
            for(j=0;j<childMatrixLength;j++)
                {receiveDataRow[i][j]=A[i][j];
                 receiveDataCol[i][j]=B[i][j];

                }
        while(messageCount!=(int)pow(nproc,0.5))
        {
            //printf("messageCount:%d\n",messageCount);
            //printf("abs %d\n",abs(rank/matrixLength-rank%matrixLength));
            //if(messageCount==abs(rank/matrixLength-rank%matrixLength))//prpcess(i,j)(j>=i) send data to the process of ith row.
               if(rank==((rank/(int)pow(nproc,0.5)*(int)pow(nproc,0.5)
                +rank/(int)pow(nproc,0.5)+messageCount-rank/(int)pow(nproc,0.5)*(int)pow(nproc,0.5))
                %(int)pow(nproc,0.5)+rank/(int)pow(nproc,0.5)*(int)pow(nproc,0.5))) 
                {
                    printf("sendrank:%d  messageCount: %d\n",rank,messageCount);
                    int i=-1;
                    int start=rank/(int)pow(nproc,0.5)*(int)pow(nproc,0.5);
                    for(i=start;i<start+(int)pow(nproc,0.5);i++)
                        {

                                MPI_Request request;
                                MPI_Isend(A,childMatrixLength*childMatrixLength,MPI_INT,i,rank,MPI_COMM_WORLD,&request);


                        }
                }

            MPI_Status status;
            int sourceProcessRow=((rank/(int)pow(nproc,0.5))*(int)pow(nproc,0.5)+
                rank/(int)pow(nproc,0.5)+messageCount-(rank/(int)pow(nproc,0.5))*(int)pow(nproc,0.5))
            %(int)pow(nproc,0.5)+(rank/(int)pow(nproc,0.5))*(int)pow(nproc,0.5);

            //printf("A:rank:%d from %d\n",rank,sourceProcessRow);

            MPI_Recv(receiveDataRow,childMatrixLength*childMatrixLength,MPI_INT,sourceProcessRow,sourceProcessRow,MPI_COMM_WORLD,&status);//receive row data                    

                //C=C+receiveDataRow*receiveDataCol;//********
                int i=-1;
                int j=-1;
                int ci=-1;
                for(i=0;i<childMatrixLength;i++)
                    for(j=0;j<childMatrixLength;j++)
                        for(ci=0;ci<childMatrixLength;ci++)
                            C[i][j]=C[i][j]+receiveDataRow[i][ci]*receiveDataCol[ci][j];


                messageCount++;
                //printf("rank: %d messageCount: %d\n",rank,messageCount);

            int destProcess=(rank/(int)pow(nproc,0.5)-1+(int)pow(nproc,0.5))%(int)pow(nproc,0.5)*(int)pow(nproc,0.5)+rank%(int)pow(nproc,0.5);

            MPI_Request request;
            MPI_Isend(receiveDataCol,childMatrixLength*childMatrixLength,MPI_INT,destProcess,rank,MPI_COMM_WORLD,&request);//Every process(i,j) send matrixB to(i-1,j) 


            int sourceProcessCol=(rank/(int)pow(nproc,0.5)+1)%(int)pow(nproc,0.5)*(int)pow(nproc,0.5)+rank%(int)pow(nproc,0.5);

            MPI_Recv(receiveDataCol,childMatrixLength*childMatrixLength,MPI_INT,sourceProcessCol,sourceProcessCol,MPI_COMM_WORLD,&status);//Every process(i,j) receive matrixB to(i+1,j) 

            //printf("C[%d,%d]=+A[%d,%d]*B[%d,%d]\n",row,col,sourceProcessRow/(int)pow(nproc,0.5),sourceProcessRow%(int)pow(nproc,0.5),sourceProcessCol/(int)pow(nproc,0.5)-1,sourceProcessCol%(int)pow(nproc,0.5));

        }

    int allValue[matrixLength*matrixLength];
    MPI_Gather(C,childMatrixLength*childMatrixLength,MPI_INT,allValue,childMatrixLength*childMatrixLength, MPI_INT, 0,MPI_COMM_WORLD);


    ///problem!!!!!!!!!!!!!!!!!
    if(0==rank)
        {int ultimate[matrixLength][matrixLength];  
         int tempCount=0;
        for(i=0;i<pow(nproc,0.5);i++)
            for(j=0;j<pow(nproc,0.5);j++)
                {int row=-1;
                 int col=-1;
                 for(row=i*childMatrixLength;row<(i+1)*childMatrixLength;row++)
                    for(col=j*childMatrixLength;col<(j+1)*childMatrixLength;col++)
                        {
                         ultimate[row][col]=allValue[tempCount];
                         tempCount++;
                        }

                }

        printf("************************\n");
        for(i=0;i<matrixLength;i++)
            {for(j=0;j<matrixLength;j++)
                printf("%d ",ultimate[i][j]);
             printf("\n");
            }           

        printf("************************\n");
        }



    MPI_Finalize();
    return 0;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用MPI(Message Passing Interface)来实现粒子群优化算法(PSO)的多维优化问题。PSO是一种启发式优化算法,用求解复杂的多维优化问题。 MPI是一种通信协议和库,用于在并行计算环境中进行进程间通信。在MPI中,你可以创建多个进程,每个进程负责处理一个子问题。这些进程可以通过消息传递来共享信息和协调计算。 对于实现PSO算法的多维优化问题,你可以按照以下步骤使用MPI: 1. 初始化MPI:首先,你需要初始化MPI环境,并获取进程数量和当前进程的标识符。 2. 初始化粒子群:在每个进程中,你需要初始化一组粒子的初始位置和速度。可以随机生成初始位置,并根据问题的特定要求设置初始速度。 3. 计算适应度:对于每个粒子,你需要计算其适应度值。适应度函数应根据问题的特定要求进行定义。 4. 更新粒子位置和速度:根据PSO算法的更新规则,更新每个粒子的位置和速度。这可以通过使用适应度函数来评估新位置,并根据当前位置、速度和全局最优解来更新速度。 5. 通信和协调:在每次迭代后,你需要通过MPI的通信机制来协调粒子之间的信息交换。例如,你可以使用MPI的发送和接收函数来传递全局最优解的信息。 6. 终止条件:设置适当的终止条件,例如达到最大迭代次数或满足特定的收敛条件。 7. 最优解提取:在算法结束后,你可以通过比较每个进程中的最优解,找到全局最优解。 需要注意的是,MPI并行化的PSO算法需要合理地划分问题空间,并确保进程之间的通信和同步。此外,你还需要考虑如何处理边界条件、收敛性以及其他问题特定的细节。 希望以上步骤能够帮助你使用MPI实现多维优化问题的粒子群算法!如果有任何进一步的问题,请随时向我提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值