首页 > 解决方案 > Vigenere 上 MPI 错误 MPI_COMM_WORLD 无法加密或解密

问题描述

我是使用 open-mpi 的新手,我想使用 MPI 来解决 viginere 密码问题,我的问题是: 1. 甚至不排队 2. 在我插入要加密的单词和密钥后,出现此错误

[mpi-VirtualBox:1646] *** An error occurred in MPI_Recv
[mpi-VirtualBox:1646] *** reported by process [2610495489,0]
[mpi-VirtualBox:1646] *** on communicator MPI_COMM_WORLD
[mpi-VirtualBox:1646] *** MPI_ERR_RANK: invalid rank
[mpi-VirtualBox:1646] *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
[mpi-VirtualBox:1646] ***    and potentially your MPI job)

这是我的代码

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <time.h>
void my_bcast(void* data, int count, MPI_Datatype datatype, int root,
              MPI_Comm communicator) {
  int world_rank;
  MPI_Comm_rank(communicator, &world_rank);
  int world_size;
  MPI_Comm_size(communicator, &world_size);

  if (world_rank == root) {
    // If we are the root process, send our data to everyone
    int i;
    for (i = 0; i < world_size; i++) {
      if (i != world_rank) {
        MPI_Send(data, count, datatype, i, 0, communicator);
      }
    }
  } else {
    // If we are a receiver process, receive the data from the root
    MPI_Recv(data, count, datatype, root, 0, communicator, MPI_STATUS_IGNORE);
  }
}
    int k,j,lenPesan, lenKunci;
    char pesan[1000];
    char kunci[1000];
    char kunciBaru[1000];
    char encryptedPesan[1000];
    char decryptedPesan[1000];
int main(int argc, char** argv) {
  MPI_Init(NULL, NULL);
  int world_rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  int world_size;
  MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  if (world_rank == 0) {
    int i;
    for (i=0; i<world_size;i++){
    printf("Program Vigenere Cipher\n");
    printf("Encryption dan Decryption\n");
    printf("Menggunakan implementasi Message Passing Computing\n");
    printf("--------------------------------------------------\n");
    printf("Masukkan Pesan (huruf besar tanpa spasi) = ");
    scanf("%s",pesan);
    printf("\nMasukkan Key (huruf kecil tanpa spasi) = ");
    scanf("%s",kunci);
    char kunciBaru[lenPesan],encryptedPesan[lenPesan],decryptedPesan[lenPesan];
    lenPesan = strlen(pesan);
    lenKunci = strlen(kunci);
    for(k = 0, j = 0; k < lenPesan; ++k, ++j){
        if(j == lenKunci)
            j = 0;
            kunciBaru[k] = kunci[j];
        }
        kunciBaru[k] = '\0';
    MPI_Status status;
    my_bcast(&pesan, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    my_bcast(&kunci, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    my_bcast(&lenPesan, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    my_bcast(&lenKunci, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    my_bcast(&kunciBaru, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    MPI_Recv(&encryptedPesan, 1, MPI_CHAR, i, 0, MPI_COMM_WORLD, &status);
    MPI_Recv(&decryptedPesan, 1, MPI_CHAR, i, 0, MPI_COMM_WORLD, &status);
    }
    printf("Original Message: %s", pesan);
        printf("\nKey: %s", kunci);
        printf("\nNew Generated Key: %s", kunciBaru);
        printf("\nEncrypted Message: %s", encryptedPesan);
        printf("\nDecrypted Message: %s\n", decryptedPesan);
        return 0;

  } else {
    my_bcast(&pesan, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    my_bcast(&kunci, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    my_bcast(&lenPesan, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    my_bcast(&lenKunci, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
    my_bcast(&kunciBaru, 1, MPI_CHAR, 0, MPI_COMM_WORLD);

    for(k = 0; k < lenPesan; ++k)
    encryptedPesan[k] = ((pesan[k] + kunciBaru[k]) % 26) + 'A';
    encryptedPesan[k] = '\0';
    MPI_Send(&encryptedPesan, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);

    for(k = 0; k < lenPesan; ++k)
    decryptedPesan[k] = (((encryptedPesan[k] - kunciBaru[k]) + 26) % 26) + 'A';
    decryptedPesan[k] = '\0';
    MPI_Send(&decryptedPesan, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
  }

  MPI_Finalize();
}

到目前为止,我已经尝试将源进程的等级更改为 root,问题仍然存在,将源进程的等级更改为我仍然有同样的问题。我知道这是一团糟请不要评判我。如果有人可以提供帮助,我将非常感谢您的帮助,对不起我的语言不好。谢谢

标签: cmpi

解决方案


推荐阅读