int main() { // struct _arr_random arrobject; // Initarr(100,0); // printf_s("%d\n",returnmax()); // printf_s("%d\n",returnmin()); // printf_s("%d\n ",countnum()); // printfall(); // printf_allnum(); int a[]={1,22,4,56,78}; insertsort(a,5); printf_all(a,5); getchar(); return 0; }
//---------------------------------------------------------------- void CommsInit( ) { int grid_periodicity[NDIM] = {1,1,1,1,1}; /* Array used to specify periodic BCs */ int pe_reorder = 0; /* Flag to disallow PE reordering for the cart-comm */ // If we have already been initialized, don't try to do it twice: if( Is_Initialised ) return; // Set-up the default values for the comms parameters: RNGseed = 1; strcpy(seedFileName,"rng.dat"); strcpy(logFileName, "stderr"); // If MPI has not already been started somewhere else, start it here. int initialised_remotely; MPI_Initialized(&initialised_remotely); if(initialised_remotely){ if(!grid_is_set) RaiseError("MPISCU_CommsInit: Processor grid must be set using MPISCU::set_pe_grid."); }else{ int dummy_argc; char **dummy_argv; MPI_Init(&dummy_argc, &dummy_argv); grid_is_set = true; // just in case. } // Check the environment variable MPISCU::ENVVAR. If it is defined and // is a non-zero length string, then that is either the name of a file // from which to read the read the parameters, or it is the parameters // themselves. If it is defined but zero length, then we use the // default parameter filename. // If it is not defined, then MPISCU::peGrid had better be // initialised already. char *envvar = getenv(ENVVAR); if(envvar){ if(strlen(envvar) == 0) envvar = default_filename; ParseCommsParam(envvar); } /* Define the (cartesian, periodic) topology of the MPI communicator */ MPI_Cart_create( MPI_COMM_WORLD, /* Original communicator */ NDIM, /* No. dimensions */ peGrid, /* No. PEs in each direction */ grid_periodicity, /* Periodicity of PE grid in each direction */ pe_reorder, /* True/false flag to allow PE re-ordering */ &Cart_Comm /* The new, cartesian, communicator */ ); /* Look up process number */ MPI_Comm_rank( Cart_Comm, &peRank ); /* Look-up processor position */ MPI_Cart_coords( Cart_Comm, peRank, NDIM, pePos ); /* Look up number of processors */ MPI_Comm_size( Cart_Comm, &peNum ); #define MPISCU_DEBUG #ifdef MPISCU_DEBUG /* Initialise the log-file, which may actually be stdout or stderr */ if( strcmp(logFileName,"stderr") == 0 ) logFile = stderr; else if( strcmp(logFileName,"stdout") == 0 ) logFile = stdout; else { /* Create a logfile name with the PE number as a suffix, such that we have logfile.01, logfile.02 ... logfile.15 etc */ /* ordPEnum is no. of orders of magnitude (base10) of number of PEs */ int ordPEnum = 1 + (int)log10(peNum); char *PEnum = (char*)malloc( ( ordPEnum + 1 ) * sizeof(char) ); strcat(logFileName,"."); /* Create filenumber based on PE number + leading zeros */ sprintf(PEnum,"%i", ((int)(exp(((double)ordPEnum)*log(10.0))))+peRank); strcat(logFileName,&PEnum[1]); /* Skip the leading 1 */ free( PEnum ); /* Open the logfile associated with this PE */ logFile = Fopen(logFileName,"w"); } #endif /* Inform user that initialization has started */ printf_all("MPISCU::CommsInit: Initializing.\n"); /* identify the root processor as that which lies at pos[i]=0 forall i */ /* calculate the identifier on every process */ int root_check = 0; for(int idirn = 0; idirn < NDIM; idirn++ ) root_check+=pePos[idirn]; /* Gather the values of root_check from every PE onto every PE */ int *root_array = (int*)malloc(peNum*sizeof(int)); if( root_array == NULL ) RaiseError("MPISCU::CommsInit: malloc failed for root_array."); MPI_Allgather( &root_check, /* Pointer to number to be gathered */ 1, /* i.e. gathering a single item */ MPI_INT, /* which is a standard C integer */ root_array, /* Pointer to the array which will recieve the data */ 1, /* One thing from each PE */ MPI_INT, /* and that thing is an int. */ Cart_Comm /* Using the cartesian communicator */ ); /* Every PE goes through the list and identifies the root PE */ for(int ir = 0; ir < peNum; ir++ ) if( root_array[ir] == 0 ) root_pe = ir; /* Free the memory associated with the root-checking array */ free(root_array); /* Log that the initialization has completed and give this PEs rank */ #ifdef MPISCU_DEBUG fprintf_all(logFile,"MPISCU::CommsInit: Initialization complete [PE=%i of %i, ROOT_PE=%i].\n",peRank, peNum, root_pe ); #endif /* Initialise the MPI Request handler */ ReqMan = new MPIRequestManager(); /* Initialise the table of NNs, indexed by SCUDir */ int dir_index, dummy; for( int idim = 0; idim < NDIM; idim++ ) { for( int idir = -1; idir <=1 ; idir+=2 ) { if( idim == 0 && idir == +1 ) dir_index = SCU_TP; if( idim == 0 && idir == -1 ) dir_index = SCU_TM; if( idim == 1 && idir == +1 ) dir_index = SCU_XP; if( idim == 1 && idir == -1 ) dir_index = SCU_XM; if( idim == 2 && idir == +1 ) dir_index = SCU_YP; if( idim == 2 && idir == -1 ) dir_index = SCU_YM; if( idim == 3 && idir == +1 ) dir_index = SCU_ZP; if( idim == 3 && idir == -1 ) dir_index = SCU_ZM; if( idim == 4 && idir == +1 ) dir_index = SCU_SP; if( idim == 4 && idir == -1 ) dir_index = SCU_SM; MPI_Cart_shift( Cart_Comm, // Using the cartesian communicator idim, // Do this dimension idir, // Look up nearest neighbour &dummy, // Rank of this PE &(nnList[dir_index]) // Rank of neighbour PE ); } } Is_Initialised = true; }