mpimanager.cpp
来自「open lattice boltzmann project www.open」· C++ 代码 · 共 722 行 · 第 1/2 页
CPP
722 行
}template <>void MpiManager::scatterv_impl<int>(int *sendBuf, int* sendCounts, int* displs, int* recvBuf, int recvCount, int root, MPI_Comm comm){ if (!ok) return; MPI_Scatterv(static_cast<void*>(sendBuf), sendCounts, displs, MPI_INT, static_cast<void*>(recvBuf), recvCount, MPI_INT, root, comm);}template <>void MpiManager::scatterv_impl<float>(float *sendBuf, int* sendCounts, int* displs, float* recvBuf, int recvCount, int root, MPI_Comm comm){ if (!ok) return; MPI_Scatterv(static_cast<void*>(sendBuf), sendCounts, displs, MPI_FLOAT, static_cast<void*>(recvBuf), recvCount, MPI_FLOAT, root, comm);}template <>void MpiManager::scatterv_impl<double>(double *sendBuf, int* sendCounts, int* displs, double* recvBuf, int recvCount, int root, MPI_Comm comm){ if (!ok) return; MPI_Scatterv(static_cast<void*>(sendBuf), sendCounts, displs, MPI_DOUBLE, static_cast<void*>(recvBuf), recvCount, MPI_DOUBLE, root, comm);}template <>void MpiManager::gatherv_impl<char>(char* sendBuf, int sendCount, char* recvBuf, int* recvCounts, int* displs, int root, MPI_Comm comm){ if (!ok) return; MPI_Gatherv(static_cast<void*>(sendBuf), sendCount, MPI_CHAR, static_cast<void*>(recvBuf), recvCounts, displs, MPI_CHAR, root, comm);}template <>void MpiManager::gatherv_impl<int>(int* sendBuf, int sendCount, int* recvBuf, int* recvCounts, int* displs, int root, MPI_Comm comm){ if (!ok) return; MPI_Gatherv(static_cast<void*>(sendBuf), sendCount, MPI_INT, static_cast<void*>(recvBuf), recvCounts, displs, MPI_INT, root, comm);}template <>void MpiManager::gatherv_impl<float>(float* sendBuf, int sendCount, float* recvBuf, int* recvCounts, int* displs, int root, MPI_Comm comm){ if (!ok) return; MPI_Gatherv(static_cast<void*>(sendBuf), sendCount, MPI_FLOAT, static_cast<void*>(recvBuf), recvCounts, displs, MPI_FLOAT, root, comm);}template <>void MpiManager::gatherv_impl<double>(double* sendBuf, int sendCount, double* recvBuf, int* recvCounts, int* displs, int root, MPI_Comm comm){ if (!ok) return; MPI_Gatherv(static_cast<void*>(sendBuf), sendCount, MPI_DOUBLE, static_cast<void*>(recvBuf), recvCounts, displs, MPI_DOUBLE, root, comm);}template <>void MpiManager::bCast<char>(char* sendBuf, int sendCount, int root, MPI_Comm comm){ if (!ok) return; MPI_Bcast(static_cast<void*>(sendBuf), sendCount, MPI_CHAR, root, comm);}template <>void MpiManager::bCast<int>(int* sendBuf, int sendCount, int root, MPI_Comm comm){ if (!ok) return; MPI_Bcast(static_cast<void*>(sendBuf), sendCount, MPI_INT, root, comm);}template <>void MpiManager::bCast<float>(float* sendBuf, int sendCount, int root, MPI_Comm comm){ if (!ok) return; MPI_Bcast(static_cast<void*>(sendBuf), sendCount, MPI_FLOAT, root, comm);}template <>void MpiManager::bCast<double>(double* sendBuf, int sendCount, int root, MPI_Comm comm){ if (!ok) return; MPI_Bcast(static_cast<void*>(sendBuf), sendCount, MPI_DOUBLE, root, comm);}template <>void MpiManager::bCastThroughMaster<char>(char* sendBuf, int sendCount, bool iAmRoot, MPI_Comm comm){ if (!ok) return; if (iAmRoot && !isMainProcessor()) { send(sendBuf, sendCount, 0); } if (isMainProcessor() && !iAmRoot) { receive(sendBuf, sendCount, MPI_ANY_SOURCE); } bCast(sendBuf, sendCount, 0);}template <>void MpiManager::bCastThroughMaster<int>(int* sendBuf, int sendCount, bool iAmRoot, MPI_Comm comm){ if (!ok) return; if (iAmRoot && !isMainProcessor()) { send(sendBuf, sendCount, 0); } if (isMainProcessor() && !iAmRoot) { receive(sendBuf, sendCount, MPI_ANY_SOURCE); } bCast(sendBuf, sendCount, 0);}template <>void MpiManager::bCastThroughMaster<float>(float* sendBuf, int sendCount, bool iAmRoot, MPI_Comm comm){ if (!ok) return; if (iAmRoot && !isMainProcessor()) { send(sendBuf, sendCount, 0); } if (isMainProcessor() && !iAmRoot) { receive(sendBuf, sendCount, MPI_ANY_SOURCE); } bCast(sendBuf, sendCount, 0);}template <>void MpiManager::bCastThroughMaster<double>(double* sendBuf, int sendCount, bool iAmRoot, MPI_Comm comm){ if (!ok) return; if (iAmRoot && !isMainProcessor()) { send(sendBuf, sendCount, 0); } if (isMainProcessor() && !iAmRoot) { receive(sendBuf, sendCount, MPI_ANY_SOURCE); } bCast(sendBuf, sendCount, 0);}template <>void MpiManager::reduce<char>(char sendVal, char& recvVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; MPI_Reduce(static_cast<void*>(&sendVal), static_cast<void*>(&recvVal), 1, MPI_CHAR, op, root, comm);}template <>void MpiManager::reduce<int>(int sendVal, int& recvVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; MPI_Reduce(static_cast<void*>(&sendVal), static_cast<void*>(&recvVal), 1, MPI_INT, op, root, comm);}template <>void MpiManager::reduce<float>(float sendVal, float& recvVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; MPI_Reduce(static_cast<void*>(&sendVal), static_cast<void*>(&recvVal), 1, MPI_FLOAT, op, root, comm);}template <>void MpiManager::reduce<double>(double sendVal, double& recvVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; MPI_Reduce(static_cast<void*>(&sendVal), static_cast<void*>(&recvVal), 1, MPI_DOUBLE, op, root, comm);}template <>void MpiManager::reduceVect<char>(std::vector<char>& sendVal, std::vector<char>& recvVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; MPI_Reduce(static_cast<void*>(&(sendVal[0])), static_cast<void*>(&(recvVal[0])), sendVal.size(), MPI_CHAR, op, root, comm);}template <>void MpiManager::reduceVect<int>(std::vector<int>& sendVal, std::vector<int>& recvVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; MPI_Reduce(static_cast<void*>(&(sendVal[0])), static_cast<void*>(&(recvVal[0])), sendVal.size(), MPI_INT, op, root, comm);}template <>void MpiManager::reduceVect<float>(std::vector<float>& sendVal, std::vector<float>& recvVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; MPI_Reduce(static_cast<void*>(&(sendVal[0])), static_cast<void*>(&(recvVal[0])), sendVal.size(), MPI_FLOAT, op, root, comm);}template <>void MpiManager::reduceVect<double>(std::vector<double>& sendVal, std::vector<double>& recvVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; MPI_Reduce(static_cast<void*>(&(sendVal[0])), static_cast<void*>(&(recvVal[0])), sendVal.size(), MPI_DOUBLE, op, root, comm);}template <>void MpiManager::reduceAndBcast<char>(char& reductVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; char recvVal; MPI_Reduce(&reductVal, &recvVal, 1, MPI_CHAR, op, root, comm); reductVal = recvVal; MPI_Bcast(&reductVal, 1, MPI_CHAR, root, comm);}template <>void MpiManager::reduceAndBcast<int>(int& reductVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; int recvVal; MPI_Reduce(&reductVal, &recvVal, 1, MPI_INT, op, root, comm); reductVal = recvVal; MPI_Bcast(&reductVal, 1, MPI_INT, root, comm);}template <>void MpiManager::reduceAndBcast<float>(float& reductVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; float recvVal; MPI_Reduce(&reductVal, &recvVal, 1, MPI_FLOAT, op, root, comm); reductVal = recvVal; MPI_Bcast(&reductVal, 1, MPI_FLOAT, root, comm);}template <>void MpiManager::reduceAndBcast<double>(double& reductVal, MPI_Op op, int root, MPI_Comm comm){ if (!ok) return; double recvVal; MPI_Reduce(&reductVal, &recvVal, 1, MPI_DOUBLE, op, root, comm); reductVal = recvVal; MPI_Bcast(&reductVal, 1, MPI_DOUBLE, root, comm);}void MpiManager::wait(MPI_Request* request, MPI_Status* status){ if (!ok) return; MPI_Wait(request, status);}void MpiManager::waitAll(MpiNonBlockingHelper& mpiNbHelper){ if (!ok) return; MPI_Waitall(mpiNbHelper.get_size(), mpiNbHelper.get_mpiRequest(), mpiNbHelper.get_mpiStatus());}MpiNonBlockingHelper::MpiNonBlockingHelper(){ _size = 0;}MpiNonBlockingHelper::MpiNonBlockingHelper( MpiNonBlockingHelper const& rhs ){ _size = rhs._size; if (_size!=0) { allocate(_size); for (unsigned i=0; i<_size; i++) { _mpiRequest[i] = rhs._mpiRequest[i]; _mpiStatus[i] = rhs._mpiStatus[i]; } }}MpiNonBlockingHelper MpiNonBlockingHelper::operator= ( MpiNonBlockingHelper rhs ) { MpiNonBlockingHelper tmp(rhs); return tmp;}MpiNonBlockingHelper::~MpiNonBlockingHelper(){ free();}void MpiNonBlockingHelper::allocate(unsigned i){ free(); _size = i; _mpiRequest = new MPI_Request [i]; _mpiStatus = new MPI_Status [i];}void MpiNonBlockingHelper::free(){ if (_size!=0) { delete [] _mpiRequest; delete [] _mpiStatus; _size = 0; }}unsigned const& MpiNonBlockingHelper::get_size() const{ return _size;}MPI_Request* MpiNonBlockingHelper::get_mpiRequest() const{ return _mpiRequest;}MPI_Status* MpiNonBlockingHelper::get_mpiStatus() const{ return _mpiStatus;}} // namespace singleton}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?