c - 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,问题仍然存在,将源进程的等级更改为我仍然有同样的问题。我知道这是一团糟请不要评判我。如果有人可以提供帮助,我将非常感谢您的帮助,对不起我的语言不好。谢谢
解决方案
推荐阅读
- pandas - 根据条件计算数据框中的连续出现次数
- python - 如何使用 Python 的 Pyperclip 将文本粘贴到单独的程序上?
- javascript - 检查先前的值是否已更改,如果已更改,请将更改前的先前值放入日志表中并在屏幕上打印该日志表
- tensorflow - 要建立什么样的神经网络模型来识别算法是 n*n?
- apache - 如何解释 apache mod 重写规则?
- javascript - Google Apps Script Doesn't Work When Embedded. TypeError: Cannot call method "getActiveSheet" of null
- php - 将变量从 Php 下拉列表传递到 Ajax
- sql - Hive 根据日期范围汇总表中的数据
- angular - mat-datepicker 上的多种 dateInput 格式
- python - 在 Python 中的混合效应模型中指定交叉随机效应的符号