// Created by James McClure // Copyright 2008-2013 #include #include #include #include #include #include #include // std::exception #include #include "common/Domain.h" #include "common/Array.h" #include "common/Utilities.h" #include "common/MPI_Helpers.h" #include "common/Communication.h" // Inline function to read line without a return argument static inline void fgetl( char * str, int num, FILE * stream ) { char* ptr = fgets( str, num, stream ); if ( 0 ) {char *temp = (char *)&ptr; temp++;} } /******************************************************** * Constructors/Destructor * ********************************************************/ Domain::Domain( int nx, int ny, int nz, int rnk, int npx, int npy, int npz, double lx, double ly, double lz, int BC): Nx(0), Ny(0), Nz(0), Lx(0), Ly(0), Lz(0), Volume(0), BoundaryCondition(0), Comm(MPI_COMM_NULL), sendCount_x(0), sendCount_y(0), sendCount_z(0), sendCount_X(0), sendCount_Y(0), sendCount_Z(0), sendCount_xy(0), sendCount_yz(0), sendCount_xz(0), sendCount_Xy(0), sendCount_Yz(0), sendCount_xZ(0), sendCount_xY(0), sendCount_yZ(0), sendCount_Xz(0), sendCount_XY(0), sendCount_YZ(0), sendCount_XZ(0), sendList_x(NULL), sendList_y(NULL), sendList_z(NULL), sendList_X(NULL), sendList_Y(NULL), sendList_Z(NULL), sendList_xy(NULL), sendList_yz(NULL), sendList_xz(NULL), sendList_Xy(NULL), sendList_Yz(NULL), sendList_xZ(NULL), sendList_xY(NULL), sendList_yZ(NULL), sendList_Xz(NULL), sendList_XY(NULL), sendList_YZ(NULL), sendList_XZ(NULL), sendBuf_x(NULL), sendBuf_y(NULL), sendBuf_z(NULL), sendBuf_X(NULL), sendBuf_Y(NULL), sendBuf_Z(NULL), sendBuf_xy(NULL), sendBuf_yz(NULL), sendBuf_xz(NULL), sendBuf_Xy(NULL), sendBuf_Yz(NULL), sendBuf_xZ(NULL), sendBuf_xY(NULL), sendBuf_yZ(NULL), sendBuf_Xz(NULL), sendBuf_XY(NULL), sendBuf_YZ(NULL), sendBuf_XZ(NULL), recvCount_x(0), recvCount_y(0), recvCount_z(0), recvCount_X(0), recvCount_Y(0), recvCount_Z(0), recvCount_xy(0), recvCount_yz(0), recvCount_xz(0), recvCount_Xy(0), recvCount_Yz(0), recvCount_xZ(0), recvCount_xY(0), recvCount_yZ(0), recvCount_Xz(0), recvCount_XY(0), recvCount_YZ(0), recvCount_XZ(0), recvList_x(NULL), recvList_y(NULL), recvList_z(NULL), recvList_X(NULL), recvList_Y(NULL), recvList_Z(NULL), recvList_xy(NULL), recvList_yz(NULL), recvList_xz(NULL), recvList_Xy(NULL), recvList_Yz(NULL), recvList_xZ(NULL), recvList_xY(NULL), recvList_yZ(NULL), recvList_Xz(NULL), recvList_XY(NULL), recvList_YZ(NULL), recvList_XZ(NULL), recvBuf_x(NULL), recvBuf_y(NULL), recvBuf_z(NULL), recvBuf_X(NULL), recvBuf_Y(NULL), recvBuf_Z(NULL), recvBuf_xy(NULL), recvBuf_yz(NULL), recvBuf_xz(NULL), recvBuf_Xy(NULL), recvBuf_Yz(NULL), recvBuf_xZ(NULL), recvBuf_xY(NULL), recvBuf_yZ(NULL), recvBuf_Xz(NULL), recvBuf_XY(NULL), recvBuf_YZ(NULL), recvBuf_XZ(NULL), sendData_x(NULL), sendData_y(NULL), sendData_z(NULL), sendData_X(NULL), sendData_Y(NULL), sendData_Z(NULL), sendData_xy(NULL), sendData_yz(NULL), sendData_xz(NULL), sendData_Xy(NULL), sendData_Yz(NULL), sendData_xZ(NULL), sendData_xY(NULL), sendData_yZ(NULL), sendData_Xz(NULL), sendData_XY(NULL), sendData_YZ(NULL), sendData_XZ(NULL), recvData_x(NULL), recvData_y(NULL), recvData_z(NULL), recvData_X(NULL), recvData_Y(NULL), recvData_Z(NULL), recvData_xy(NULL), recvData_yz(NULL), recvData_xz(NULL), recvData_Xy(NULL), recvData_Yz(NULL), recvData_xZ(NULL), recvData_xY(NULL), recvData_yZ(NULL), recvData_Xz(NULL), recvData_XY(NULL), recvData_YZ(NULL), recvData_XZ(NULL), id(NULL) { auto db = std::make_shared( ); db->putScalar( "BC", BC ); db->putVector( "nproc", { npx, npx, npx } ); db->putVector( "n", { nx, ny, nz } ); db->putScalar( "nspheres", 0 ); db->putVector( "L", { lx, ly, lz } ); initialize( db ); } Domain::Domain( std::shared_ptr db ): Nx(0), Ny(0), Nz(0), Lx(0), Ly(0), Lz(0), Volume(0), BoundaryCondition(0), Comm(MPI_COMM_NULL), sendCount_x(0), sendCount_y(0), sendCount_z(0), sendCount_X(0), sendCount_Y(0), sendCount_Z(0), sendCount_xy(0), sendCount_yz(0), sendCount_xz(0), sendCount_Xy(0), sendCount_Yz(0), sendCount_xZ(0), sendCount_xY(0), sendCount_yZ(0), sendCount_Xz(0), sendCount_XY(0), sendCount_YZ(0), sendCount_XZ(0), sendList_x(NULL), sendList_y(NULL), sendList_z(NULL), sendList_X(NULL), sendList_Y(NULL), sendList_Z(NULL), sendList_xy(NULL), sendList_yz(NULL), sendList_xz(NULL), sendList_Xy(NULL), sendList_Yz(NULL), sendList_xZ(NULL), sendList_xY(NULL), sendList_yZ(NULL), sendList_Xz(NULL), sendList_XY(NULL), sendList_YZ(NULL), sendList_XZ(NULL), sendBuf_x(NULL), sendBuf_y(NULL), sendBuf_z(NULL), sendBuf_X(NULL), sendBuf_Y(NULL), sendBuf_Z(NULL), sendBuf_xy(NULL), sendBuf_yz(NULL), sendBuf_xz(NULL), sendBuf_Xy(NULL), sendBuf_Yz(NULL), sendBuf_xZ(NULL), sendBuf_xY(NULL), sendBuf_yZ(NULL), sendBuf_Xz(NULL), sendBuf_XY(NULL), sendBuf_YZ(NULL), sendBuf_XZ(NULL), recvCount_x(0), recvCount_y(0), recvCount_z(0), recvCount_X(0), recvCount_Y(0), recvCount_Z(0), recvCount_xy(0), recvCount_yz(0), recvCount_xz(0), recvCount_Xy(0), recvCount_Yz(0), recvCount_xZ(0), recvCount_xY(0), recvCount_yZ(0), recvCount_Xz(0), recvCount_XY(0), recvCount_YZ(0), recvCount_XZ(0), recvList_x(NULL), recvList_y(NULL), recvList_z(NULL), recvList_X(NULL), recvList_Y(NULL), recvList_Z(NULL), recvList_xy(NULL), recvList_yz(NULL), recvList_xz(NULL), recvList_Xy(NULL), recvList_Yz(NULL), recvList_xZ(NULL), recvList_xY(NULL), recvList_yZ(NULL), recvList_Xz(NULL), recvList_XY(NULL), recvList_YZ(NULL), recvList_XZ(NULL), recvBuf_x(NULL), recvBuf_y(NULL), recvBuf_z(NULL), recvBuf_X(NULL), recvBuf_Y(NULL), recvBuf_Z(NULL), recvBuf_xy(NULL), recvBuf_yz(NULL), recvBuf_xz(NULL), recvBuf_Xy(NULL), recvBuf_Yz(NULL), recvBuf_xZ(NULL), recvBuf_xY(NULL), recvBuf_yZ(NULL), recvBuf_Xz(NULL), recvBuf_XY(NULL), recvBuf_YZ(NULL), recvBuf_XZ(NULL), sendData_x(NULL), sendData_y(NULL), sendData_z(NULL), sendData_X(NULL), sendData_Y(NULL), sendData_Z(NULL), sendData_xy(NULL), sendData_yz(NULL), sendData_xz(NULL), sendData_Xy(NULL), sendData_Yz(NULL), sendData_xZ(NULL), sendData_xY(NULL), sendData_yZ(NULL), sendData_Xz(NULL), sendData_XY(NULL), sendData_YZ(NULL), sendData_XZ(NULL), recvData_x(NULL), recvData_y(NULL), recvData_z(NULL), recvData_X(NULL), recvData_Y(NULL), recvData_Z(NULL), recvData_xy(NULL), recvData_yz(NULL), recvData_xz(NULL), recvData_Xy(NULL), recvData_Yz(NULL), recvData_xZ(NULL), recvData_xY(NULL), recvData_yZ(NULL), recvData_Xz(NULL), recvData_XY(NULL), recvData_YZ(NULL), recvData_XZ(NULL), id(NULL) { initialize( db ); } void Domain::initialize( std::shared_ptr db ) { d_db = db; auto nproc = d_db->getVector("nproc"); auto n = d_db->getVector("n"); auto L = d_db->getVector("L"); //nspheres = d_db->getScalar("nspheres"); ASSERT( n.size() == 3u ); ASSERT( L.size() == 3u ); ASSERT( nproc.size() == 3u ); int nx = n[0]; int ny = n[1]; int nz = n[2]; Lx = L[0]; Ly = L[1]; Lz = L[2]; Nx = nx+2; Ny = ny+2; Nz = nz+2; // Initialize ranks int myrank; MPI_Comm_rank( MPI_COMM_WORLD, &myrank ); rank_info = RankInfoStruct(myrank,nproc[0],nproc[1],nproc[2]); // Fill remaining variables N = Nx*Ny*Nz; Volume = nx*ny*nx*nproc[0]*nproc[1]*nproc[2]*1.0; id = new char[N]; memset(id,0,N); BoundaryCondition = d_db->getScalar("BC"); int nprocs; MPI_Comm_size( MPI_COMM_WORLD, &nprocs ); INSIST(nprocs == nproc[0]*nproc[1]*nproc[2],"Fatal error in processor count!"); } Domain::~Domain() { // Free sendList delete [] sendList_x; delete [] sendList_y; delete [] sendList_z; delete [] sendList_X; delete [] sendList_Y; delete [] sendList_Z; delete [] sendList_xy; delete [] sendList_yz; delete [] sendList_xz; delete [] sendList_Xy; delete [] sendList_Yz; delete [] sendList_xZ; delete [] sendList_xY; delete [] sendList_yZ; delete [] sendList_Xz; delete [] sendList_XY; delete [] sendList_YZ; delete [] sendList_XZ; // Free sendBuf delete [] sendBuf_x; delete [] sendBuf_y; delete [] sendBuf_z; delete [] sendBuf_X; delete [] sendBuf_Y; delete [] sendBuf_Z; delete [] sendBuf_xy; delete [] sendBuf_yz; delete [] sendBuf_xz; delete [] sendBuf_Xy; delete [] sendBuf_Yz; delete [] sendBuf_xZ; delete [] sendBuf_xY; delete [] sendBuf_yZ; delete [] sendBuf_Xz; delete [] sendBuf_XY; delete [] sendBuf_YZ; delete [] sendBuf_XZ; // Free recvList delete [] recvList_x; delete [] recvList_y; delete [] recvList_z; delete [] recvList_X; delete [] recvList_Y; delete [] recvList_Z; delete [] recvList_xy; delete [] recvList_yz; delete [] recvList_xz; delete [] recvList_Xy; delete [] recvList_Yz; delete [] recvList_xZ; delete [] recvList_xY; delete [] recvList_yZ; delete [] recvList_Xz; delete [] recvList_XY; delete [] recvList_YZ; delete [] recvList_XZ; // Free recvBuf delete [] recvBuf_x; delete [] recvBuf_y; delete [] recvBuf_z; delete [] recvBuf_X; delete [] recvBuf_Y; delete [] recvBuf_Z; delete [] recvBuf_xy; delete [] recvBuf_yz; delete [] recvBuf_xz; delete [] recvBuf_Xy; delete [] recvBuf_Yz; delete [] recvBuf_xZ; delete [] recvBuf_xY; delete [] recvBuf_yZ; delete [] recvBuf_Xz; delete [] recvBuf_XY; delete [] recvBuf_YZ; delete [] recvBuf_XZ; // Free sendData delete [] sendData_x; delete [] sendData_y; delete [] sendData_z; delete [] sendData_X; delete [] sendData_Y; delete [] sendData_Z; delete [] sendData_xy; delete [] sendData_xY; delete [] sendData_Xy; delete [] sendData_XY; delete [] sendData_xz; delete [] sendData_xZ; delete [] sendData_Xz; delete [] sendData_XZ; delete [] sendData_yz; delete [] sendData_yZ; delete [] sendData_Yz; delete [] sendData_YZ; // Free recvData delete [] recvData_x; delete [] recvData_y; delete [] recvData_z; delete [] recvData_X; delete [] recvData_Y; delete [] recvData_Z; delete [] recvData_xy; delete [] recvData_xY; delete [] recvData_Xy; delete [] recvData_XY; delete [] recvData_xz; delete [] recvData_xZ; delete [] recvData_Xz; delete [] recvData_XZ; delete [] recvData_yz; delete [] recvData_yZ; delete [] recvData_Yz; delete [] recvData_YZ; // Free id delete [] id; // Free the communicator if ( Comm != MPI_COMM_WORLD && Comm != MPI_COMM_NULL ) { MPI_Comm_free(&Comm); } } /******************************************************** * Initialize communication * ********************************************************/ void Domain::CommInit(MPI_Comm Communicator) { int i,j,k,n; int sendtag = 21; int recvtag = 21; MPI_Comm_dup(Communicator,&Comm); // set up the neighbor ranks int myrank; MPI_Comm_rank( Comm, &myrank ); rank_info = RankInfoStruct( myrank, rank_info.nx, rank_info.ny, rank_info.nz ); MPI_Barrier(Communicator); //...................................................................................... MPI_Request req1[18], req2[18]; MPI_Status stat1[18],stat2[18]; //...................................................................................... sendCount_x = sendCount_y = sendCount_z = sendCount_X = sendCount_Y = sendCount_Z = 0; sendCount_xy = sendCount_yz = sendCount_xz = sendCount_Xy = sendCount_Yz = sendCount_xZ = 0; sendCount_xY = sendCount_yZ = sendCount_Xz = sendCount_XY = sendCount_YZ = sendCount_XZ = 0; //...................................................................................... for (k=1; k Label; vector Affinity; // Read the labels if (rank()==0){ printf("Component labels:\n"); ifstream iFILE("ComponentLabels.csv"); if (iFILE.good()){ int value; while (!iFILE.eof()){ iFILE>>value; iFILE>>AFFINITY; VALUE=char(value); Label.push_back(value); Affinity.push_back(AFFINITY); NLABELS++; printf("%i %f\n",VALUE,AFFINITY); } } else{ printf("Using default labels: Solid (0 --> -1.0), NWP (1 --> 1.0), WP (2 --> -1.0)\n"); // Set default values VALUE=0; AFFINITY=-1.0; Label.push_back(VALUE); Affinity.push_back(AFFINITY); NLABELS++; VALUE=1; AFFINITY=1.0; Label.push_back(VALUE); Affinity.push_back(AFFINITY); NLABELS++; VALUE=2; AFFINITY=-1.0; Label.push_back(VALUE); Affinity.push_back(AFFINITY); NLABELS++; } } MPI_Barrier(Comm); // Broadcast the list MPI_Bcast(&NLABELS,1,MPI_INT,0,Comm); //printf("rank=%i, NLABELS=%i \n ",rank(),NLABELS); // Copy into contiguous buffers char *LabelList; double * AffinityList; LabelList=new char[NLABELS]; AffinityList=new double[NLABELS]; if (rank()==0){ for (int idx=0; idx < NLABELS; idx++){ VALUE=Label[idx]; AFFINITY=Affinity[idx]; printf("rank=%i, idx=%i, value=%d, affinity=%f \n",rank(),idx,VALUE,AFFINITY); LabelList[idx]=VALUE; AffinityList[idx]=AFFINITY; } } MPI_Barrier(Comm); MPI_Bcast(LabelList,NLABELS,MPI_CHAR,0,Comm); MPI_Bcast(AffinityList,NLABELS,MPI_DOUBLE,0,Comm); // Assign the labels for (int k=0;k