我写了一个函数来完成它。它没有到位,也没有写得特别好,但为了后代,我会添加我的解决方案。我意识到我真的不需要重塑标准 C++ 数组,但我会把它留在那里,直到它成为我的问题。
int ReshapeMultiVector(Epetra_MultiVector *&reshapedMultiVec, const int length, const int numVecs, const Epetra_MultiVector &originalMultiVec, Epetra_MpiComm &comm){
/* Function to reshape a multivector. Does it by copying the values, so additional memory will be required.
* Note that this function does NOT delete the original vector.
* INPUTS
* reshapedMultiVec - This is a reference to a null pointer that will point to the
* new (reshaped) multivector.
* length - Global length of the new multivector
* numVecs - The number of vectors in the new multivector.
* originalMultiVec - The original multivector that is being reshaped.
* comm - MPI communicator
* OUTPUT
* int - Error flag
*/
int err;
int numVecsOrig = originalMultiVec.NumVectors();
int lengthOrig = originalMultiVec.GlobalLength();
//extract phase_gradx and phase_grady as standard arrays so that we can reshape them.
double** originalMultiVec_std = new double*[numVecsOrig];
for(int i=0;i<numVecsOrig;++i){
originalMultiVec_std[i] = new double[lengthOrig];
}
err = MultiVectorToArray(originalMultiVec_std, originalMultiVec, false);
if(err) return err;
// Copy the values to all the processors after extracting them.
for(int i=0;i<numVecsOrig;++i){
comm.Broadcast(originalMultiVec_std[i], lengthOrig, 0);
}
double** reshaped_std = new double*[numVecs];
for(int i=0;i<numVecs;++i){
reshaped_std[i] = new double[length];
}
err = Reshape2DArray(originalMultiVec_std, lengthOrig, numVecsOrig, reshaped_std, length, numVecs);
if(err) return err;
Epetra_Map reshapedMap(length,0,comm);
//Initialize the vector
int numMyElements = reshapedMap.NumMyElements();
int* myGlobalElements = reshapedMap.MyGlobalElements();
reshapedMultiVec = new Epetra_MultiVector(reshapedMap,numVecs);
double ** Ap = reshapedMultiVec->Pointers();
for (int j=0; j<numVecs; ++j)
{
double * v = Ap[j];
// Fill it
for (int i=0; i<numMyElements; ++i)
{
v[i] = reshaped_std[j][myGlobalElements[i]];
}
}
// free memory
for(int i=0;i<numVecsOrig;++i){
delete[] originalMultiVec_std[i];
}
delete[] originalMultiVec_std;
for(int i=0;i<numVecs;++i){
delete[] reshaped_std[i];
}
delete[] reshaped_std;
return(EXIT_SUCCESS);
}