ここでは面白い問題があります。私はすでにいくつかのサブ配列をソートしており、ソートされた大きな配列にそれらをマージする必要があります。私は以下のコードでこれをやろうとしていますが、私が期待している結果は得られません。C++ MPI:std :: merge on arrays
あなたのうちの誰かが私が間違っていることを教えてもらえましたか?
あなたが見ることができますどのように#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
#define N 32
#define ROOT 0
int A[N]; // this should be global
void quickSort(int*, int, int);
int partition(int*, int, int);
int main(int argc, char *argv[]) {
int size;
int rank;
vector<int> result(N);
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int count = N/size;
int *localArray = (int *) malloc(count * sizeof(int));
if (rank == ROOT) {
for (int i = 0; i < N; i++) {
A[i] = rand() % 10;
}
// master local copy
for (int i = 0; i < count; i++)
localArray[i] = A[i];
for (int dest = 1; dest < size; ++dest) {
MPI_Send(&A[dest * count], count, MPI_INT, dest, 1, MPI_COMM_WORLD);
printf("P0 sent a %d elements to P%d.\n", count, dest);
}
int source = 1;
int sizeResult = count * 2;
int sizeResult2 = count;
int tmpVec[sizeResult2];
int tm[sizeResult];
MPI_Recv(tmpVec, count, MPI_INT, source, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (int source = 2; source < size; source++) {
MPI_Recv(localArray, count, MPI_INT, source, 2, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
//-------------------------------HERE IS THE PROBLEM---------------------------
merge(tmpVec, tmpVec + sizeResult2, localArray, localArray + count, tm);
sizeResult2 = sizeResult;
for (int i = 0; i < sizeResult; i++) {
tmpVec[i] = tm[i];
cout << tm[i] << " ";
}
cout << endl;
sizeResult += count;
//-------------------------------------------------------------------------------
}
for (int i = 0; i < sizeResult2; i++)
cout << tmpVec[i] << " ";
cout << endl << sizeResult2 << endl;
for (int i = 0; i < N; i++)
cout << A[i] << " ";
}
else {
MPI_Recv(localArray, count, MPI_INT, ROOT, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
quickSort(localArray, 0, count);
MPI_Send(localArray, count, MPI_INT, ROOT, 2, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
void quickSort(int* A, int p, int q) {
int r;
if (p < q) {
r = partition(A, p, q);
quickSort(A, p, r);
quickSort(A, r + 1, q);
}
}
int partition(int* A, int p, int q) {
int x = A[p];
int i = p;
int j;
for (j = p + 1; j < q; j++) {
if (A[j] <= x) {
i = i + 1;
swap(A[i], A[j]);
}
}
swap(A[i], A[p]);
return i;
}
、私がした後に、第2の1の最初の部分配列をマージしようとしている:私は見当がつかないので、私のロジックが、それはよさそうだだ、私は...ここ
が私のコードだと思います
間違って何かなり明白ですが、実際の結果が期待される結果と異なる方法を説明する際に、より注意して下さい。 "*私は期待している結果を得られない*。"は通常十分ではありません。 – Zulan