void Matrix::multiply(const int *l1, const int *c1, const int *l2, const int *c2) { /*Так как c1 и l2 имеют одинаковое значение (столбец 1-го и строка 2-го массива), используется только одна переменная.*/ initializeArray(l1, c2); for (int i = 0; i < *l1; i++) for (int j = 0; j < *c2; j++) { array[i][j] = 0; for (int m = 0; m < *l2; m++) array[i][j] += arr1[i][m] * arr2[m][j]; } }
BBArray *bbArrayNew( const char *type,int dims,... ){ #if BB_ARGP int *lens=(int*)bbArgp(8); #else int *lens=&dims+1; #endif BBArray *arr=allocateArray( type,dims,lens ); initializeArray( arr ); return arr; }
void ValarrayVersion(BenchmarkExt<int>& bench, double u) { bench.beginImplementation("valarray<T>"); while (!bench.doneImplementationBenchmark()) { int N = bench.getParameter(); cout << "valarray<T>: N = " << N << endl; cout.flush(); long iters = bench.getIterations(); valarray<double> x(N); initializeArray(x, N); valarray<double> a(N); initializeArray(a, N); valarray<double> b(N); initializeArray(b, N); valarray<double> c(N); initializeArray(c, N); bench.start(); for (long i=0; i < iters; ++i) { x=u+a+b+c; sink(); } bench.stop(); bench.startOverhead(); for (long i=0; i < iters; ++i) sink(); bench.stopOverhead(); } bench.endImplementation(); }
int main () { // matrix of 4 rows and 3 columns int matrix[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; int** pmat = (int **)matrix; printf("&matrix[0][0] = %u\n", &matrix[0][0]); printf("&pmat[0][0] = %u\n", &pmat[0][0]); initializeArray(-1); return 0; }
z3::expr Z3ArrayBuilder::getInitialArray(const Array *root) { z3::expr result(context_); if (cache_->findArray(root, result)) return result; char buf[256]; snprintf(buf, sizeof(buf), "%s_%p", root->name.c_str(), (void*)root); result = context_.constant(buf, context_.array_sort(context_.bv_sort(32), context_.bv_sort(8))); if (root->isConstantArray()) { result = initializeArray(root, result); } cache_->insertArray(root, result); return result; }
int main () { int i; charArray1 = initializeArray((char*)calloc(SIZE+1,sizeof(char))); charArray2 = (char*)calloc(SIZE+1,sizeof(char)); for (i = 0; i < SIZE; i++) charArray2[SIZE-1-i] = charArray1[i]; charArray2[SIZE] = '\0'; printf("The ASCII table in reverse is:\n%s\n",charArray2); free(charArray2); free(charArray1); return(0); }
int main() { do { string fileName = getFileName(); vector<int> numberArray; initializeArray(fileName, numberArray); cout << "The lowest number is: " << getLowest(numberArray) << endl; cout << "The highest number is: " << getHighest(numberArray) << endl; cout << "The sum of all these numbers is: " << getSum(numberArray) << endl; cout << "The average of all these numbers is: " << setprecision(10) << fixed << getAverage(numberArray) << endl; } while (!wantToExit()); return 0; }
/* Concept - - we will give a vertex, and in this function we are going to calculate the diatance of all other connected node, from this vertex. Algo - - First set the distance array to -1, for all the vertex. */ void ShortestPath(int vertex) { initializeArray(); m_distance[vertex] = 0; // distance to self is 0 MyQueue<char> q; q.enQueue(m_graphLookup[vertex]); char vertexTemp; while (!q.isEmpty()) { vertexTemp = q.deQueue(); for (int index = 0; index < m_vertex; ++index) { //if (m_matrix[vertex][index]) //{ // m_distance[index] = //} } } }
int getToken(){ int character; Array** value; while(1){ switch(character = getchar()){ /* operators */ case '^': fprintf( stderr, "[OP:%c]", character); return character; case '.': fprintf(stderr, "[OP:%c]", character); return character; /* brackets */ case '(': case ')': fprintf(stderr, "[OP:%c]", character); return character; /* whitespace */ case ' ': case '\t': continue; /* newline */ case '\n': fprintf(stderr, "%c", character); return character; /* the rest */ default: /* numbers */ if(isdigit(character)){ value = initializeArray(value); do { addArrayElement(value, (character - '0')); } while (isdigit(character = getchar())); ungetc(character, stdin); fprintf(stderr, "[NUM:%d]", value); currentAttribute = value; return NUM; } else error("Illegal character"); } } }
int main(int argc, const char* argv[]) { if( argc != 2 ) //checks for the input file name { printf( "error; no input file name\n" ); return 1; } FILE *filePointer; filePointer = fopen( argv[1], "r" ); char **arrayPointer; //char maze[MAX_MAZE_SIZE][MAX_MAZE_SIZE] = { 0 }; int numberOfTestCases = 0; fscanf( filePointer, "%d\n", &numberOfTestCases ); for( int testCaseNumber = 0; testCaseNumber < numberOfTestCases; testCaseNumber++ ) { int size = 0; int * xy; int x; int y; fscanf( filePointer, "%d\n", &size ); printf( "ENTER\n" ); allocateArray(size, &arrayPointer); initializeArray(size, arrayPointer, filePointer); xy = findEntrance(size, arrayPointer); x = xy[0]; y = xy[1]; solveMaze(size, arrayPointer, x ,y); deallocateArray(size, arrayPointer); printf( "EXIT\n***\n" ); } fclose( filePointer ); return 0; }
int main (int args, char *argsv []) { int array[20]; initializeArray (array); organizeArray (array); int n; printf ("Enter a number\n"); scanf ("%i", &n); int index1, index2; if (!isSumOfArray (array, n, &index1, &index2)) { printf ("There are no shuch pair of numbers\n"); } else { printf ("data[%i] + data[%i] = %i + %i = %i\n", index1, index2, array[index1], array[index2], n); } return(0); }
int main() { srand(time(NULL)); // For a proper random number(Not nessesary) int i; int myArray[10]; // Describing variables int indexesArray[10]; for(i=0; i<10; i++){ indexesArray[i] = i; // We gave the value to our indexes } // from 0 to 9 initializeArray(myArray); // We are calling our initializer array ///////////////////////////////////// printf("\nNORMAL LIST\n"); // This is where we print our // printf("-----------\n"); // Normal list, our random numbers // printf("indexes\tnumbers\n"); // with their indexes. // // // for (i=0 ; 10>i ; i++){ // // printf("%d\t",indexesArray[i]); // // printf("%d\n", myArray[i]); // // } ///////////////////////////////////// indexSort(myArray,indexesArray); // We are calling our indexSort func. printf("\nSORTED LIST\n"); ///////////////////////////////////// printf("-----------\n"); // Again printing , but this time // printf("indexes\tnumbers\n"); // with sorted way. // // // for (i=0 ; 10>i ; i++){ // // printf("%d\t",indexesArray[i]); // // printf("%d\n", myArray[indexesArray[i]]); // } ///////////////////////////////////// return 0; }
int main() { FILE *text; char fileName[30]; // array to store file name int alphabet[26]; // array to store frequency of each letter char character; int characterNum = 0; // counter for number of characters int space = 0; // counter for number of white-space characters int value = 1; printf("Please enter the file name: "); scanf("%s", fileName); text = fopen(fileName, "r"); // read-only initializeArray(alphabet); /*loop to scan each character*/ while(value != 0) { // loop until break condition fscanf(text, "%c", &character); if(feof(text)) // break condition when end of file is reached value = 0; else if(isalpha(character)) // if character is a letter characterNum = letter(character, alphabet, characterNum); else if(isspace(character)) { // if character is white space space++; characterNum++; } else characterNum++; } frequencyTable(alphabet); data(alphabet, characterNum, space); return 0; }
//Creates the random music array (fist time through) and retrieves the // filepath of the next music file to play. void get_random_file(char *filetype, char *path) { DIR_ITER *dir = NULL; char filename[1024]; int cnt = 0; int next = 0; struct stat filestat; if (fileCount==0 && !first_time) goto out; dbg_printf(gt("Music: Looking for %s files in: %s"), filetype, path); dbg_printf("\n"); // Open directory //snprintf(dirname, sizeof(dirname), "%s", path); dir = diropen(path); if (!dir) return; if (first_time) { while (!dirnext(dir, filename, &filestat)) { // Ignore invalid entries if (match_ext(filename, filetype) && !(filestat.st_mode & S_IFDIR)) fileCount++; } dbg_printf(gt("Music: Number of %s files found: %i"), filetype, fileCount); dbg_printf("\n"); first_time = false; //if no files found then no need to continue if (fileCount==0) goto out; //allocate the random music array musicArray = realloc(musicArray, fileCount * sizeof(int)); if (!musicArray) { fileCount = 0; goto out; } initializeArray(musicArray, fileCount); randomizeArray(musicArray, fileCount); //check array contents int i; dbg_printf(gt("Music: musicArray contents: ")); for (i=0; i<fileCount; i++) dbg_printf("%i ", musicArray[i]); dbg_printf("\n"); //reset the directory dirreset(dir); } if (fileCount > 0) { //get the next file index lastPlayed++; if (lastPlayed > fileCount-1) lastPlayed = 0; next = musicArray[lastPlayed]; dbg_printf(gt("Music: Next file index to play: %i"), next); dbg_printf("\n"); //iterate through and find our file while (!dirnext(dir, filename, &filestat)) { if (match_ext(filename, filetype) && !(filestat.st_mode & S_IFDIR)) { cnt++; if (cnt==next) { //save path snprintf(music_fname, sizeof(music_fname), "%s/%s", path, filename); goto out; } } } } out:; //close the directory dirclose(dir); }
int main(int argc, char* argv[]) { long int s, it; unsigned int flag, verbose; unsigned int NXPROB, NYPROB; double start, end; int iz; // create file and verbose flags flag = 0; verbose = 0; // Parse command line options int opt; char *file = NULL; while ((opt = getopt(argc, argv, "hvs:f:")) != -1) { switch (opt) { case 'v': verbose = 1; break; case 's': if( !(s=atoi(optarg)) ) { fprintf(stderr, "Cannot parse %s value.\n", optarg); exit(EXIT_FAILURE); } break; case 'f': file = optarg; flag = 1; break; case 'h': default: fprintf(stderr, "Usage: %s [-s SIZE] [-f output file]\n", argv[0]); exit(EXIT_FAILURE); } } // Set initial data values NXPROB = NX - 1; NYPROB = NY - 1; if(verbose) { fprintf(stdout, "[INFO] Setting map size to %d (%dx%d)\n", NX*NY, NX, NY); fprintf(stdout, "[INFO] Max iter %d\n", MAXSTEP); } if(verbose && flag) { fprintf(stdout, "[INFO] Using output file %s\n", file); } // Program starts here // Set initial and boundary conditions initializeArray (X1, NX, NY); setupBoundaryConditions(X1, NX, NY); setupBoundaryConditions(X2, NX, NY); // Main calculations iz = 0; for (it = 0; it < MAXSTEP; it++) { if(verbose && (it%(MAXSTEP/10) == 0)) { fprintf(stdout, "[INFO] iteration %ld, time %.3f seconds\n", it, gettime()-start); } long int i, j; for (i = 1; i < NXPROB; i++) { for (j = 1; j < NYPROB; j++) { X2[i][j] = X1[i][j] + CX * ( X1[i+1][j] + X1[i-1][j] - 2.0 * X1[i][j] ) + CY * ( X1[i][j+1] + X1[i][j-1] - 2.0 * X1[i][j] ); } } iz = 1 - iz; } // Save output file if(flag) save(X2, NX, NY, file); // End time end = gettime(); // Get information: wall clock time, problem size, ... if(verbose) { fprintf(stdout, "[INFO] Convergence after %d steps\n", MAXSTEP); fprintf(stdout, "[INFO] Problem size %d [%dx%d]\n", NY*NX, NX, NY); fprintf(stdout, "[INFO] Wall clock time %lf seconds\n",(end-start)); if(flag) fprintf(stdout, "[INFO] Output file %s\n", file); } else { printf("Time %.3f seconds, Size %d [%dx%d]\n", end - start, NY*NX, NX, NY); } exit(EXIT_SUCCESS); }
void BPlusIndexP::splitAddInner(IndexPage* page, Index* index, IndexPage* rightPage) { Logger* log = getLogger(NULL); //temporal arrays Index** tmpelements = (Index**)malloc(sizeof(Index*) * (BUCKET_MAX_ELEMENTS + 1)); IndexPage** tmppointers = (IndexPage**)malloc(sizeof(IndexPage*) * (BUCKET_MAX_ELEMENTS + 2)); initializeArray((void**)tmpelements, BUCKET_MAX_ELEMENTS); initializeArray((void**)tmppointers, BUCKET_MAX_ELEMENTS + 1); copyArray((void**)page->elements, (void**)tmpelements, 0, BUCKET_MAX_ELEMENTS - 1, 0); copyArray((void**)page->pointers, (void**)tmppointers, 0, BUCKET_MAX_ELEMENTS, 0); int posToInsert = findInsertPositionArray(tmpelements, index, page->size, BUCKET_MAX_ELEMENTS); insertArray((void**)tmpelements, index, posToInsert, BUCKET_MAX_ELEMENTS + 1); insertArray((void**)tmppointers, rightPage, posToInsert + 1, BUCKET_MAX_ELEMENTS + 2); // clean the previous "left" initializeArray((void**)page->elements, BUCKET_MAX_ELEMENTS); initializeArray((void**)page->pointers, BUCKET_MAX_ELEMENTS + 1); IndexPage* newRightPage = new IndexPage(); int midPoint = (BUCKET_MAX_ELEMENTS / 2); copyArray((void**)tmpelements, (void**)page->elements, 0, midPoint, 0); copyArray((void**)tmppointers, (void**)page->pointers, 0, midPoint + 1, 0); page->size = (BUCKET_MAX_ELEMENTS / 2) + 1; copyArray((void**)tmpelements, (void**)newRightPage->elements, midPoint + 1, BUCKET_MAX_ELEMENTS, 0); copyArray((void**)tmppointers, (void**)newRightPage->pointers, midPoint + 2, BUCKET_MAX_ELEMENTS + 1, 1); // Clean up the elements moved to the rightPage for (int x = page->size; x < BUCKET_MAX_ELEMENTS; x++) { page->elements[x] = NULL; page->pointers[x + 1] = NULL; } // cleans the last one page->pointers[BUCKET_MAX_ELEMENTS] = NULL; refreshParentRelationship(page); newRightPage->size = (BUCKET_MAX_ELEMENTS / 2) + 1; refreshParentRelationship(newRightPage); // Promotion IndexPage* parentElement = page->parentElement; Index* element = newRightPage->elements[0]; if (parentElement == NULL) { createRoot(element, page, newRightPage); } else { addElement(parentElement, element, newRightPage); } parentElement = newRightPage->parentElement; shiftLeftArray((void**)newRightPage->elements, 0, 1, BUCKET_MAX_ELEMENTS - 1); shiftLeftArray((void**)newRightPage->pointers, 0, 1, BUCKET_MAX_ELEMENTS); newRightPage->size--; refreshParentRelationship(parentElement); free(tmpelements); free(tmppointers); persistPage(page); persistPage(newRightPage); persistPage(page->parentElement); }
int main() { //Declares all of the arrays that are to be used char inFileName[MAXFILENAMELENGTH]; char outFileName[MAXFILENAMELENGTH]; int fileContents[NUMINTSINFILE]; int addedContents[NUMINTSAFTERADDITION]; int subtractedContents[NUMINTSAFTERSUBTRACTION]; //Initializes the arrays to 0s initializeArray(addedContents, NUMINTSAFTERADDITION); initializeArray(fileContents, NUMINTSINFILE); initializeArray(subtractedContents, NUMINTSAFTERSUBTRACTION); //Declares the file streams to be used ifstream inFile; ofstream outFile; //Gets the input file name cout << "Please enter the file name to open: "; cin >> inFileName; //Gets the output file name cout << "Please enter the file name to write to: "; cin >> outFileName; //Reads in the values from the input file name to the input values array inFile.open(inFileName); if (!inFile.fail()) { cout << "Input file found." << endl; getIntValues(fileContents, NUMINTSINFILE, inFile); toScreen(fileContents, NUMINTSINFILE); } else { cout << "Input file not found. Ending program." << endl; return 1; } //Adds the values per instruction into another array addValues(fileContents, addedContents, NUMINTSINFILE, NUMINTSAFTERADDITION); //Subtracts the values per instruction into another array subtractValues(fileContents, subtractedContents, NUMINTSINFILE, NUMINTSAFTERSUBTRACTION); //Outputs the added and subtracted values to the output file name outFile.open(outFileName); if (!outFile.fail()) { cout << "Output file found." << endl; toScreen(addedContents, NUMINTSAFTERADDITION); toScreen(subtractedContents, NUMINTSAFTERSUBTRACTION); outFile << "Added values: " << endl; for (int i = 0; i < NUMINTSAFTERADDITION; i++) { outFile << addedContents[i] << " "; } outFile << "\nSubtracted values: " << endl; for (int i = 0; i < NUMINTSAFTERSUBTRACTION; i++) { outFile << subtractedContents[i] << " "; } } else { cout << "Output file not found. Ending program." << endl; return 1; } //Closes the file streams as to clean up system inFile.close(); outFile.close(); return 0; }
int dynamixMain (int argc, char * argv[]) { //// DECLARING VARIABLES // Struct of parameters PARAMETERS p; // CVode variables void * cvode_mem = NULL; // pointer to block of CVode memory N_Vector y, yout; // arrays of populations // arrays for energetic parameters realtype ** V = NULL; // pointer to k-c coupling constants realtype * Vbridge = NULL; // pointer to array of bridge coupling constants. // first element [0] is Vkb1, last [Nb] is VcbN realtype * Vnobridge = NULL; // coupling constant when there is no bridge //// Setting defaults for parameters to be read from input //// done setting defaults int flag; realtype * k_pops = NULL; // pointers to arrays of populations realtype * l_pops = NULL; realtype * c_pops = NULL; realtype * b_pops = NULL; realtype * ydata = NULL; // pointer to ydata (contains all populations) realtype * wavefunction = NULL; // (initial) wavefunction realtype * dm = NULL; // density matrix realtype * dmt = NULL; // density matrix in time realtype * wfnt = NULL; // density matrix in time realtype * k_energies = NULL; // pointers to arrays of energies realtype * c_energies = NULL; realtype * b_energies = NULL; realtype * l_energies = NULL; realtype t0 = 0.0; // initial time realtype t = 0; realtype tret = 0; // time returned by the solver time_t startRun; // time at start of log time_t endRun; // time at end of log struct tm * currentTime = NULL; // time structure for localtime #ifdef DEBUG FILE * realImaginary; // file containing real and imaginary parts of the wavefunction #endif FILE * log; // log file with run times realtype * tkprob = NULL; // total probability in k, l, c, b states at each timestep realtype * tlprob = NULL; realtype * tcprob = NULL; realtype * tbprob = NULL; double ** allprob = NULL; // populations in all states at all times realtype * times = NULL; realtype * qd_est = NULL; realtype * qd_est_diag = NULL; std::string inputFile = "ins/parameters.in"; // name of input file std::string cEnergiesInput = "ins/c_energies.in"; std::string cPopsInput = "ins/c_pops.in"; std::string bEnergiesInput = "ins/b_energies.in"; std::string VNoBridgeInput = "ins/Vnobridge.in"; std::string VBridgeInput = "ins/Vbridge.in"; std::map<const std::string, bool> outs; // map of output file names to bool // default output directory p.outputDir = "outs/"; double summ = 0; // sum variable // ---- process command line flags ---- // opterr = 0; int c; std::string insDir; /* process command line options */ while ((c = getopt(argc, argv, "i:o:")) != -1) { switch (c) { case 'i': // check that it ends in a slash std::cerr << "[dynamix]: assigning input directory" << std::endl; insDir = optarg; if (strcmp(&(insDir.at(insDir.length() - 1)), "/")) { std::cerr << "ERROR: option -i requires argument (" << insDir << ") to have a trailing slash (/)." << std::endl; return 1; } else { // ---- assign input files ---- // inputFile = insDir + "parameters.in"; cEnergiesInput = insDir + "c_energies.in"; cPopsInput = insDir + "c_pops.in"; bEnergiesInput = insDir + "b_energies.in"; VNoBridgeInput = insDir + "Vnobridge.in"; VBridgeInput = insDir + "Vbridge.in"; } break; case 'o': std::cerr << "[dynamix]: assigning output directory" << std::endl; p.outputDir = optarg; break; case '?': if (optopt == 'i') { fprintf(stderr, "Option -%c requires a directory argument.\n", optopt); } else if (isprint(optopt)) { fprintf(stderr, "Unknown option -%c.\n", optopt); } else { fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); } return 1; default: continue; } } optind = 1; // reset global variable counter for the next time this is run std::cerr << "[dynamix]: ARGUMENTS" << std::endl; for (int ii = 0; ii < argc; ii++) { std::cerr << "[dynamix]: " << argv[ii] << std::endl; } //// ASSIGN PARAMETERS FROM INPUT FILE // ---- TODO create output directory if it does not exist ---- // flag = mkdir(p.outputDir.c_str(), 0755); std::cerr << "Looking for inputs in all the " << inputFile << " places" << std::endl; assignParams(inputFile.c_str(), &p); // Decide which output files to make #ifdef DEBUG std::cout << "Assigning outputs as specified in " << inputFile << "\n"; #endif assignOutputs(inputFile.c_str(), outs, &p); #ifdef DEBUG // print out which outputs will be made for (std::map<const std::string, bool>::iterator it = outs.begin(); it != outs.end(); it++) { std::cout << "Output file: " << it->first << " will be created.\n"; } #endif // OPEN LOG FILE; PUT IN START TIME // if (isOutput(outs, "log.out")) { log = fopen("log.out", "w"); // note that this file is closed at the end of the program } time(&startRun); currentTime = localtime(&startRun); if (isOutput(outs, "log.out")) { fprintf(log, "Run started at %s\n", asctime(currentTime)); } if (isOutput(outs, "log.out")) { // make a note about the laser intensity. fprintf(log,"The laser intensity is %.5e W/cm^2.\n\n",pow(p.pumpAmpl,2)*3.5094452e16); } //// READ DATA FROM INPUTS p.Nc = numberOfValuesInFile(cEnergiesInput.c_str()); p.Nb = numberOfValuesInFile(bEnergiesInput.c_str()); k_pops = new realtype [p.Nk]; c_pops = new realtype [p.Nc]; b_pops = new realtype [p.Nb]; l_pops = new realtype [p.Nl]; k_energies = new realtype [p.Nk]; c_energies = new realtype [p.Nc]; b_energies = new realtype [p.Nb]; l_energies = new realtype [p.Nl]; if (numberOfValuesInFile(cPopsInput.c_str()) != p.Nc) { fprintf(stderr, "ERROR [Inputs]: c_pops and c_energies not the same length.\n"); return -1; } readArrayFromFile(c_energies, cEnergiesInput.c_str(), p.Nc); if (p.bridge_on) { if (p.bridge_on && (p.Nb < 1)) { std::cerr << "\nERROR: bridge_on but no bridge states. The file b_energies.in is probably empty.\n"; return -1; } p.Vbridge.resize(p.Nb+1); readArrayFromFile(b_energies, bEnergiesInput.c_str(), p.Nb); readVectorFromFile(p.Vbridge, VBridgeInput.c_str(), p.Nb + 1); #ifdef DEBUG std::cout << "COUPLINGS:"; for (int ii = 0; ii < p.Nb+1; ii++) { std::cout << " " << p.Vbridge[ii]; } std::cout << std::endl; #endif } else { p.Nb = 0; p.Vnobridge.resize(1); readVectorFromFile(p.Vnobridge, VNoBridgeInput.c_str(), 1); } #ifdef DEBUG std::cout << "\nDone reading things from inputs.\n"; #endif //// PREPROCESS DATA FROM INPUTS // check torsion parameters, set up torsion spline if (p.torsion) { #ifdef DEBUG std::cout << "Torsion is on." << std::endl; #endif // error checking if (p.torsionSite > p.Nb) { std::cerr << "ERROR: torsion site (" << p.torsionSite << ") is larger than number of bridge sites (" << p.Nb << ")." << std::endl; exit(-1); } else if (p.torsionSite < 0) { std::cerr << "ERROR: torsion site is less than zero." << std::endl; exit(-1); } if (!fileExists(p.torsionFile)) { std::cerr << "ERROR: torsion file " << p.torsionFile << " does not exist." << std::endl; } // create spline p.torsionV = new Spline(p.torsionFile.c_str()); if (p.torsionV->getFirstX() != 0.0) { std::cerr << "ERROR: time in " << p.torsionFile << " should start at 0.0." << std::endl; exit(-1); } if (p.torsionV->getLastX() < p.tout) { std::cerr << "ERROR: time in " << p.torsionFile << " should be >= tout." << std::endl; exit(-1); } } // set number of processors for OpenMP //omp_set_num_threads(p.nproc); mkl_set_num_threads(p.nproc); p.NEQ = p.Nk+p.Nc+p.Nb+p.Nl; // total number of equations set p.NEQ2 = p.NEQ*p.NEQ; // number of elements in DM #ifdef DEBUG std::cout << "\nTotal number of states: " << p.NEQ << std::endl; std::cout << p.Nk << " bulk, " << p.Nc << " QD, " << p.Nb << " bridge, " << p.Nl << " bulk VB.\n"; #endif tkprob = new realtype [p.numOutputSteps+1]; // total population on k, b, c at each timestep tcprob = new realtype [p.numOutputSteps+1]; tbprob = new realtype [p.numOutputSteps+1]; tlprob = new realtype [p.numOutputSteps+1]; allprob = new double * [p.numOutputSteps+1]; for (int ii = 0; ii <= p.numOutputSteps; ii++) { allprob[ii] = new double [p.NEQ]; } // assign times. p.times.resize(p.numOutputSteps+1); for (int ii = 0; ii <= p.numOutputSteps; ii++) { p.times[ii] = float(ii)/p.numOutputSteps*p.tout; } qd_est = new realtype [p.numOutputSteps+1]; qd_est_diag = new realtype [p.numOutputSteps+1]; p.Ik = 0; // set index start positions for each type of state p.Ic = p.Nk; p.Ib = p.Ic+p.Nc; p.Il = p.Ib+p.Nb; // assign bulk conduction and valence band energies // for RTA, bulk and valence bands have parabolic energies if (p.rta) { buildParabolicBand(k_energies, p.Nk, p.kBandEdge, CONDUCTION, &p); buildParabolicBand(l_energies, p.Nl, p.lBandTop, VALENCE, &p); } else { buildContinuum(k_energies, p.Nk, p.kBandEdge, p.kBandTop); buildContinuum(l_energies, p.Nl, p.kBandEdge - p.valenceBand - p.bulk_gap, p.kBandEdge - p.bulk_gap); } // calculate band width p.kBandWidth = k_energies[p.Nk - 1] - k_energies[0]; //// BUILD INITIAL WAVEFUNCTION // bridge states (empty to start) initializeArray(b_pops, p.Nb, 0.0); // coefficients in bulk and other states depend on input conditions in bulk if (!p.rta) { #ifdef DEBUG std::cout << "\ninitializing k_pops\n"; #endif if (p.bulk_constant) { initializeArray(k_pops, p.Nk, 0.0); #ifdef DEBUG std::cout << "\ninitializing k_pops with constant probability in range of states\n"; #endif initializeArray(k_pops+p.Nk_first-1, p.Nk_final-p.Nk_first+1, 1.0); initializeArray(l_pops, p.Nl, 0.0); // populate l states (all 0 to start off) initializeArray(c_pops, p.Nc, 0.0); // QD states empty to start } else if (p.bulk_Gauss) { buildKPopsGaussian(k_pops, k_energies, p.kBandEdge, p.bulkGaussSigma, p.bulkGaussMu, p.Nk); // populate k states with FDD initializeArray(l_pops, p.Nl, 0.0); // populate l states (all 0 to start off) initializeArray(c_pops, p.Nc, 0.0); // QD states empty to start } else if (p.qd_pops) { readArrayFromFile(c_pops, cPopsInput.c_str(), p.Nc); // QD populations from file initializeArray(l_pops, p.Nl, 0.0); // populate l states (all 0 to start off) initializeArray(k_pops, p.Nk, 0.0); // populate k states (all zero to start off) } else { initializeArray(k_pops, p.Nk, 0.0); // populate k states (all zero to start off) initializeArray(l_pops, p.Nl, 1.0); // populate l states (all populated to start off) initializeArray(c_pops, p.Nc, 0.0); // QD states empty to start } #ifdef DEBUG std::cout << "\nThis is k_pops:\n"; for (int ii = 0; ii < p.Nk; ii++) { std::cout << k_pops[ii] << std::endl; } std::cout << "\n"; #endif } // with RTA, use different set of switches else { // bulk valence band if (p.VBPopFlag == POP_EMPTY) { #ifdef DEBUG std::cout << "Initializing empty valence band" << std::endl; #endif initializeArray(l_pops, p.Nl, 0.0); } else if (p.VBPopFlag == POP_FULL) { #ifdef DEBUG std::cout << "Initializing full valence band" << std::endl; #endif initializeArray(l_pops, p.Nl, 1.0); } else { std::cerr << "ERROR: unrecognized VBPopFlag " << p.VBPopFlag << std::endl; } // bulk conduction band if (p.CBPopFlag == POP_EMPTY) { #ifdef DEBUG std::cout << "Initializing empty conduction band" << std::endl; #endif initializeArray(k_pops, p.Nk, 0.0); } else if (p.CBPopFlag == POP_FULL) { #ifdef DEBUG std::cout << "Initializing full conduction band" << std::endl; #endif initializeArray(k_pops, p.Nk, 1.0); } else if (p.CBPopFlag == POP_CONSTANT) { #ifdef DEBUG std::cout << "Initializing constant distribution in conduction band" << std::endl; #endif initializeArray(k_pops, p.Nk, 0.0); initializeArray(k_pops, p.Nk, 1e-1); // FIXME initializeArray(k_pops+p.Nk_first-1, p.Nk_final-p.Nk_first+1, 1.0); } else if (p.CBPopFlag == POP_GAUSSIAN) { #ifdef DEBUG std::cout << "Initializing Gaussian in conduction band" << std::endl; #endif buildKPopsGaussian(k_pops, k_energies, p.kBandEdge, p.bulkGaussSigma, p.bulkGaussMu, p.Nk); } else { std::cerr << "ERROR: unrecognized CBPopFlag " << p.CBPopFlag << std::endl; } //// QD if (p.QDPopFlag == POP_EMPTY) { initializeArray(c_pops, p.Nc, 0.0); } else if (p.QDPopFlag == POP_FULL) { initializeArray(c_pops, p.Nc, 1.0); } else { std::cerr << "ERROR: unrecognized QDPopFlag " << p.QDPopFlag << std::endl; } } // create empty wavefunction wavefunction = new realtype [2*p.NEQ]; initializeArray(wavefunction, 2*p.NEQ, 0.0); // assign real parts of wavefunction coefficients (imaginary are zero) for (int ii = 0; ii < p.Nk; ii++) { wavefunction[p.Ik + ii] = k_pops[ii]; } for (int ii = 0; ii < p.Nc; ii++) { wavefunction[p.Ic + ii] = c_pops[ii]; } for (int ii = 0; ii < p.Nb; ii++) { wavefunction[p.Ib + ii] = b_pops[ii]; } for (int ii = 0; ii < p.Nl; ii++) { wavefunction[p.Il + ii] = l_pops[ii]; } if (isOutput(outs, "psi_start.out")) { outputWavefunction(wavefunction, p.NEQ); } // Give all coefficients a random phase if (p.random_phase) { float phi; // set the seed if (p.random_seed == -1) { srand(time(NULL)); } else { srand(p.random_seed); } for (int ii = 0; ii < p.NEQ; ii++) { phi = 2*3.1415926535*(float)rand()/(float)RAND_MAX; wavefunction[ii] = wavefunction[ii]*cos(phi); wavefunction[ii + p.NEQ] = wavefunction[ii + p.NEQ]*sin(phi); } } #ifdef DEBUG // print out details of wavefunction coefficients std::cout << std::endl; for (int ii = 0; ii < p.Nk; ii++) { std::cout << "starting wavefunction: Re[k(" << ii << ")] = " << wavefunction[p.Ik + ii] << std::endl; } for (int ii = 0; ii < p.Nc; ii++) { std::cout << "starting wavefunction: Re[c(" << ii << ")] = " << wavefunction[p.Ic + ii] << std::endl; } for (int ii = 0; ii < p.Nb; ii++) { std::cout << "starting wavefunction: Re[b(" << ii << ")] = " << wavefunction[p.Ib + ii] << std::endl; } for (int ii = 0; ii < p.Nl; ii++) { std::cout << "starting wavefunction: Re[l(" << ii << ")] = " << wavefunction[p.Il + ii] << std::endl; } for (int ii = 0; ii < p.Nk; ii++) { std::cout << "starting wavefunction: Im[k(" << ii << ")] = " << wavefunction[p.Ik + ii + p.NEQ] << std::endl; } for (int ii = 0; ii < p.Nc; ii++) { std::cout << "starting wavefunction: Im[c(" << ii << ")] = " << wavefunction[p.Ic + ii + p.NEQ] << std::endl; } for (int ii = 0; ii < p.Nb; ii++) { std::cout << "starting wavefunction: Im[b(" << ii << ")] = " << wavefunction[p.Ib + ii + p.NEQ] << std::endl; } for (int ii = 0; ii < p.Nl; ii++) { std::cout << "starting wavefunction: Im[l(" << ii << ")] = " << wavefunction[p.Il + ii + p.NEQ] << std::endl; } std::cout << std::endl; summ = 0; for (int ii = 0; ii < 2*p.NEQ; ii++) { summ += pow(wavefunction[ii],2); } std::cout << "\nTotal population is " << summ << "\n\n"; #endif //// ASSEMBLE ARRAY OF ENERGIES // TODO TODO p.energies.resize(p.NEQ); for (int ii = 0; ii < p.Nk; ii++) { p.energies[p.Ik + ii] = k_energies[ii]; } for (int ii = 0; ii < p.Nc; ii++) { p.energies[p.Ic + ii] = c_energies[ii]; } for (int ii = 0; ii < p.Nb; ii++) { p.energies[p.Ib + ii] = b_energies[ii]; } for (int ii = 0; ii < p.Nl; ii++) { p.energies[p.Il + ii] = l_energies[ii]; } #ifdef DEBUG for (int ii = 0; ii < p.NEQ; ii++) { std::cout << "p.energies[" << ii << "] is " << p.energies[ii] << "\n"; } #endif //// ASSIGN COUPLING CONSTANTS V = new realtype * [p.NEQ]; for (int ii = 0; ii < p.NEQ; ii++) { V[ii] = new realtype [p.NEQ]; } buildCoupling(V, &p, outs); if (isOutput(outs, "log.out")) { // make a note in the log about system timescales double tau = 0; // fundamental system timescale if (p.Nk == 1) { fprintf(log, "\nThe timescale (tau) is undefined (Nk == 1).\n"); } else { if (p.bridge_on) { if (p.scale_bubr) { tau = 1.0/(2*p.Vbridge[0]*M_PI); } else { tau = ((p.kBandTop - p.kBandEdge)/(p.Nk - 1))/(2*pow(p.Vbridge[0],2)*M_PI); } } else { if (p.scale_buqd) { tau = 1.0/(2*p.Vnobridge[0]*M_PI); } else { tau = ((p.kBandTop - p.kBandEdge)/(p.Nk - 1))/(2*pow(p.Vnobridge[0],2)*M_PI); } } fprintf(log, "\nThe timescale (tau) is %.9e a.u.\n", tau); } } //// CREATE DENSITY MATRIX if (! p.wavefunction) { // Create the initial density matrix dm = new realtype [2*p.NEQ2]; initializeArray(dm, 2*p.NEQ2, 0.0); #pragma omp parallel for for (int ii = 0; ii < p.NEQ; ii++) { // diagonal part dm[p.NEQ*ii + ii] = pow(wavefunction[ii],2) + pow(wavefunction[ii + p.NEQ],2); if (p.coherent) { // off-diagonal part for (int jj = 0; jj < ii; jj++) { // real part of \rho_{ii,jj} dm[p.NEQ*ii + jj] = wavefunction[ii]*wavefunction[jj] + wavefunction[ii+p.NEQ]*wavefunction[jj+p.NEQ]; // imaginary part of \rho_{ii,jj} dm[p.NEQ*ii + jj + p.NEQ2] = wavefunction[ii]*wavefunction[jj+p.NEQ] - wavefunction[jj]*wavefunction[ii+p.NEQ]; // real part of \rho_{jj,ii} dm[p.NEQ*jj + ii] = dm[p.NEQ*ii + jj]; // imaginary part of \rho_{jj,ii} dm[p.NEQ*jj + ii + p.NEQ2] = -1*dm[p.NEQ*ii + jj + p.NEQ*p.NEQ]; } } } // Create the array to store the density matrix in time dmt = new realtype [2*p.NEQ2*(p.numOutputSteps+1)]; initializeArray(dmt, 2*p.NEQ2*(p.numOutputSteps+1), 0.0); #ifdef DEBUG2 // print out density matrix std::cout << "\nDensity matrix without normalization:\n\n"; for (int ii = 0; ii < p.NEQ; ii++) { for (int jj = 0; jj < p.NEQ; jj++) { fprintf(stdout, "(%+.1e,%+.1e) ", dm[p.NEQ*ii + jj], dm[p.NEQ*ii + jj + p.NEQ2]); } fprintf(stdout, "\n"); } #endif // Normalize the DM so that populations add up to 1. // No normalization if RTA is on. if (!p.rta) { summ = 0.0; for (int ii = 0; ii < p.NEQ; ii++) { // assume here that diagonal elements are all real summ += dm[p.NEQ*ii + ii]; } if ( summ == 0.0 ) { std::cerr << "\nFATAL ERROR [populations]: total population is 0!\n"; return -1; } if (summ != 1.0) { // the variable 'summ' is now a multiplicative normalization factor summ = 1.0/summ; for (int ii = 0; ii < 2*p.NEQ2; ii++) { dm[ii] *= summ; } } #ifdef DEBUG std::cout << "\nThe normalization factor for the density matrix is " << summ << "\n\n"; #endif } // Error checking for total population; recount population first summ = 0.0; for (int ii = 0; ii < p.NEQ; ii++) { summ += dm[p.NEQ*ii + ii]; } if ( fabs(summ-1.0) > 1e-12 && (!p.rta)) { std::cerr << "\nWARNING [populations]: After normalization, total population is not 1, it is " << summ << "!\n"; } #ifdef DEBUG std::cout << "\nAfter normalization, the sum of the populations in the density matrix is " << summ << "\n\n"; #endif // Add initial DM to parameters. p.startDM.resize(2*p.NEQ2); memcpy(&(p.startDM[0]), &(dm[0]), 2*p.NEQ2*sizeof(double)); } // wavefunction else { // Create the array to store the wavefunction in time wfnt = new realtype [2*p.NEQ*(p.numOutputSteps+1)]; initializeArray(wfnt, 2*p.NEQ*(p.numOutputSteps+1), 0.0); // normalize summ = 0.0; for (int ii = 0; ii < p.NEQ; ii++) { summ += pow(wavefunction[ii],2) + pow(wavefunction[ii+p.NEQ],2); } #ifdef DEBUG std::cout << "Before normalization, the total population is " << summ << std::endl; #endif summ = 1.0/sqrt(summ); for (int ii = 0; ii < 2*p.NEQ; ii++) { wavefunction[ii] *= summ; } // check total population summ = 0.0; for (int ii = 0; ii < p.NEQ; ii++) { summ += pow(wavefunction[ii],2) + pow(wavefunction[ii+p.NEQ],2); } #ifdef DEBUG std::cout << "After normalization, the total population is " << summ << std::endl; #endif if (fabs(summ - 1.0) > 1e-12) { std::cerr << "WARNING: wavefunction not normalized! Total density is " << summ << std::endl; } // Add initial wavefunction to parameters. p.startWfn.resize(2*p.NEQ); memcpy(&(p.startWfn[0]), &(wavefunction[0]), 2*p.NEQ*sizeof(double)); } //// BUILD HAMILTONIAN // //TODO TODO #ifdef DEBUG fprintf(stderr, "Building Hamiltonian.\n"); #endif realtype * H = NULL; H = new realtype [p.NEQ2]; for (int ii = 0; ii < p.NEQ2; ii++) { H[ii] = 0.0; } buildHamiltonian(H, p.energies, V, &p); // add Hamiltonian to p p.H.resize(p.NEQ2); for (int ii = 0; ii < p.NEQ2; ii++) { p.H[ii] = H[ii]; } // create sparse version of H p.H_sp.resize(p.NEQ2); p.H_cols.resize(p.NEQ2); p.H_rowind.resize(p.NEQ2 + 1); int job [6] = {0, 0, 0, 2, p.NEQ2, 1}; int info = 0; mkl_ddnscsr(&job[0], &(p.NEQ), &(p.NEQ), &(p.H)[0], &(p.NEQ), &(p.H_sp)[0], &(p.H_cols)[0], &(p.H_rowind)[0], &info); //// SET UP CVODE VARIABLES #ifdef DEBUG std::cout << "\nCreating N_Vectors.\n"; if (p.wavefunction) { std::cout << "\nProblem size is " << 2*p.NEQ << " elements.\n"; } else { std::cout << "\nProblem size is " << 2*p.NEQ2 << " elements.\n"; } #endif // Creates N_Vector y with initial populations which will be used by CVode// if (p.wavefunction) { y = N_VMake_Serial(2*p.NEQ, wavefunction); } else { y = N_VMake_Serial(2*p.NEQ2, dm); } // put in t = 0 information if (! p.wavefunction) { updateDM(y, dmt, 0, &p); } else { updateWfn(y, wfnt, 0, &p); } // the vector yout has the same dimensions as y yout = N_VClone(y); #ifdef DEBUG realImaginary = fopen("real_imaginary.out", "w"); #endif // Make plot files makePlots(outs, &p); // only do propagation if not just making plots if (! p.justPlots) { // Make outputs independent of time propagation computeGeneralOutputs(outs, &p); // create CVode object // this is a stiff problem, I guess? #ifdef DEBUG std::cout << "\nCreating cvode_mem object.\n"; #endif cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); flag = CVodeSetUserData(cvode_mem, (void *) &p); #ifdef DEBUG std::cout << "\nInitializing CVode solver.\n"; #endif // initialize CVode solver // if (p.wavefunction) { //flag = CVodeInit(cvode_mem, &RHS_WFN, t0, y); flag = CVodeInit(cvode_mem, &RHS_WFN_SPARSE, t0, y); } else { if (p.kinetic) { flag = CVodeInit(cvode_mem, &RHS_DM_RELAX, t0, y); } else if (p.rta) { flag = CVodeInit(cvode_mem, &RHS_DM_RTA, t0, y); //flag = CVodeInit(cvode_mem, &RHS_DM_RTA_BLAS, t0, y); } else if (p.dephasing) { flag = CVodeInit(cvode_mem, &RHS_DM_dephasing, t0, y); } else { //flag = CVodeInit(cvode_mem, &RHS_DM, t0, y); flag = CVodeInit(cvode_mem, &RHS_DM_BLAS, t0, y); } } #ifdef DEBUG std::cout << "\nSpecifying integration tolerances.\n"; #endif // specify integration tolerances // flag = CVodeSStolerances(cvode_mem, p.reltol, p.abstol); #ifdef DEBUG std::cout << "\nAttaching linear solver module.\n"; #endif // attach linear solver module // if (p.wavefunction) { flag = CVDense(cvode_mem, 2*p.NEQ); } else { // Diagonal approximation to the Jacobian saves memory for large systems flag = CVDiag(cvode_mem); } //// CVODE TIME PROPAGATION #ifdef DEBUG std::cout << "\nAdvancing the solution in time.\n"; #endif for (int ii = 1; ii <= p.numsteps; ii++) { t = (p.tout*((double) ii)/((double) p.numsteps)); flag = CVode(cvode_mem, t, yout, &tret, 1); #ifdef DEBUGf std::cout << std::endl << "CVode flag at step " << ii << ": " << flag << std::endl; #endif if ((ii % (p.numsteps/p.numOutputSteps) == 0) || (ii == p.numsteps)) { // show progress in stdout if (p.progressStdout) { fprintf(stdout, "\r%-.2lf percent done", ((double)ii/((double)p.numsteps))*100); fflush(stdout); } // show progress in a file if (p.progressFile) { std::ofstream progressFile("progress.tmp"); progressFile << ((double)ii/((double)p.numsteps))*100 << " percent done." << std::endl; progressFile.close(); } if (p.wavefunction) { updateWfn(yout, wfnt, ii*p.numOutputSteps/p.numsteps, &p); } else { updateDM(yout, dmt, ii*p.numOutputSteps/p.numsteps, &p); } } } #ifdef DEBUG fclose(realImaginary); #endif //// MAKE FINAL OUTPUTS // finalize log file // time(&endRun); currentTime = localtime(&endRun); if (isOutput(outs, "log.out")) { fprintf(log, "Final status of 'flag' variable: %d\n\n", flag); fprintf(log, "Run ended at %s\n", asctime(currentTime)); fprintf(log, "Run took %.3g seconds.\n", difftime(endRun, startRun)); fclose(log); // note that the log file is opened after variable declaration } if (p.progressStdout) { printf("\nRun took %.3g seconds.\n", difftime(endRun, startRun)); } // Compute density outputs. #ifdef DEBUG std::cout << "Computing outputs..." << std::endl; #endif if (p.wavefunction) { computeWfnOutput(wfnt, outs, &p); } else { computeDMOutput(dmt, outs, &p); } #ifdef DEBUG std::cout << "done computing outputs" << std::endl; #endif // do analytical propagation if (p.analytical && (! p.bridge_on)) { computeAnalyticOutputs(outs, &p); } } //// CLEAN UP #ifdef DEBUG fprintf(stdout, "Deallocating N_Vectors.\n"); #endif // deallocate memory for N_Vectors // N_VDestroy_Serial(y); N_VDestroy_Serial(yout); #ifdef DEBUG fprintf(stdout, "Freeing CVode memory.\n"); #endif // free solver memory // CVodeFree(&cvode_mem); #ifdef DEBUG fprintf(stdout, "Freeing memory in main.\n"); #endif // delete all these guys delete [] tkprob; delete [] tlprob; delete [] tcprob; delete [] tbprob; for (int ii = 0; ii <= p.numOutputSteps; ii++) { delete [] allprob[ii]; } delete [] allprob; delete [] k_pops; delete [] c_pops; delete [] b_pops; delete [] l_pops; if (p.bridge_on) { delete [] Vbridge; } else { delete [] Vnobridge; } delete [] k_energies; delete [] c_energies; delete [] b_energies; delete [] l_energies; delete [] wavefunction; delete [] H; for (int ii = 0; ii < p.NEQ; ii++) { delete [] V[ii]; } delete [] V; if (p.wavefunction) { delete [] wfnt; } else { delete [] dm; delete [] dmt; } delete [] times; delete [] qd_est; delete [] qd_est_diag; std::cout << "whoo" << std::endl; return 0; }
int main(int argc, char *argv[]) { initializeSigHandler(); setpgid(0, 0); int i = 0; int ret = 0; pid_t pid; cmpQty = argc - 2; cmp = malloc(sizeof(company*) * cmpQty); map = malloc(sizeof(graph)); sem_id = createSem(SEM_CREAT_KEY, cmpQty + SEM_QTY); setAllSem(sem_id, cmpQty + SEM_QTY, 0); if (initPIDS() == MALLOC_ERROR ) return MALLOC_ERROR; /***PARSER***/ if (cmp == NULL || map == NULL ) return parserError(NULL, NULL, MALLOC_ERROR, NULL); if ((ret = parseFiles(argc, argv, map, cmp)) != EXIT_SUCCESS ) return ret; /***INITIALIZES ARRAYS FOR THE EXECL***/ initializeArray(&semMsg, sem_id); initializeArray(&procQty, ID_QTY + cmpQty); /***MAP AND COMPANIES CREATION***/ if (createIPC(MAIN_ID, cmpQty + ID_QTY) == ERROR ) return ERROR; int map_pid = 0; int io_pid = 0; int mult_pid = 0; if ((pid = fork()) == 0) { execl("io", semMsg, procQty, NULL); exit(1); } else { io_pid = pid; addPid(io_pid); if ((pid = fork()) == 0) { execl("map", semMsg, procQty, NULL); exit(1); } else { map_pid = pid; addPid(map_pid); if ((pid = fork()) == 0) { execl("multitasker", semMsg, procQty, NULL); exit(1); } else { mult_pid = pid; addPid(mult_pid); for (i = 0; i < cmpQty; i++) { if ((pid = fork()) == 0) { char *buf = NULL; int id = CMP_ID + i; initializeArray(&buf, id); execl("company", buf, procQty, semMsg, NULL); } else { addPid(pid); } } } } } upSem(sem_id, MAIN_ID); downSem(sem_id, IO_ID); downSem(sem_id, MULTITASKER_ID); for (i = 0; i < cmpQty; i++) { downSem(sem_id, CMP_ID + i); if (initializeCmp(cmp[i], MAIN_ID, CMP_ID + i) == ERROR ) return ERROR; } downSem(sem_id, MAP_ID); if (initializeMap(map, MAIN_ID, MAP_ID) == ERROR) { return ERROR; } downSem(sem_id, MAP_ID); for (i = 0; i < cmpQty; i++) { wait(0); //Waits for all the companies to finish } kill(mult_pid, SIGTERM); waitpid(mult_pid, 0, 0); kill(map_pid, SIGTERM); waitpid(map_pid, 0, 0); kill(io_pid, SIGTERM); waitpid(io_pid, 0, 0); if (disconnectFromIPC(MAIN_ID) == ERROR) { destroyIPC(MAIN_ID); } destroySem(sem_id); /***FREES***/ freeAll(); return EXIT_SUCCESS; }
void initializeTable(MappingTable* table){ table->counter = 0; initializeArray(table->symbolArray); initializeArray(table->rankArray); }
int main(int argc, char* argv[]) { long int s, it; unsigned int flag, verbose; unsigned int NX, NY, pNX, initNX, endNX, NYPROB, offset, np, rank; double start, end; int iz; // Initialize the MPI environment MPI_Init(&argc, &argv); MPI_Request recvRequest[2], sendRequest[2]; //Control comunicació no bloquejant MPI_Comm World = MPI_COMM_WORLD; // Get the number of processes MPI_Comm_size(World, &np); // number of procs if (np<3) { printf("Error: 3 or more processes are needed (%d used)\n", np); MPI_Finalize(); exit(EXIT_FAILURE); } // Get the rank of the process MPI_Comm_rank(MPI_COMM_WORLD, &rank); // my rank in [0..P). if (rank==MASTER) fprintf(stdout, "[INFO] Processes created: %d.\n",np); // Defaut values NX = 100; NY = 100; // create file and verbose flags flag = 0; verbose = 0; // Parse command line options int opt; char *file = NULL; while ((opt = getopt(argc, argv, "hvs:f:")) != -1) { switch (opt) { case 'v': verbose = 1; break; case 's': if( !(s=atoi(optarg)) ) { fprintf(stderr, "Cannot parse %s value.\n", optarg); MPI_Finalize(); exit(EXIT_FAILURE); } NX = NY = s; break; case 'f': file = optarg; flag = 1; break; case 'h': default: fprintf(stderr, "Usage: %s [-s SIZE] [-f output file]\n", argv[0]); MPI_Finalize(); exit(EXIT_FAILURE); } } if (rank==MASTER) { if(verbose) { fprintf(stdout, "[INFO] Setting map size to %d (%dx%d)\n", NX*NY, NX, NY); fprintf(stdout, "[INFO] Max iter %d\n", MAXSTEP); } if(verbose && flag) { fprintf(stdout, "[INFO] Using output file %s\n", file); } } // Program starts here start = gettime(); // Memory allocation double** X[2]; //Partició hortizontal de la matriu X en 'np' processos pNX = floor(NX/np); if (rank==MASTER || rank==np-1) { //1r procés i últim: només s'agafa una fila de més (el 1r procés agafa //l'última fila de més, i l'últim procés agafa la primera fila de més) pNX += 1; if (rank == MASTER) { //El master és l'únic que la seva fila no és compartida, per tant no té offset offset = 0; } else { //Si la divisió NY/np no és entera l'últim procés agafarà files de més //fins el final de la matriu X pNX += NX - (floor(NX/np)*np); } } else { //procés del mig: agafa dues files de més (1a i última) pNX += 2; } //Fi partició X[0] = make2DDoubleArray (pNX, NY); X[1] = make2DDoubleArray (pNX, NY); //Càlcul de l'offset dins de X segons l'id del procés /*L'offset t'indica a quin índex de la matriu X general (algorisme seqüencial) pertany la 1a fila de la matriu X partida de cada procés (algorisme MPI) D'aquesta forma podem inicialitzar la matriu tal com ho fem en seqüencial*/ if (rank!=MASTER) //La 1a fila de tots els WORKERs es correspon realment a l'última fila del procés //anterior, per això restem una posició a l'offset. offset = floor(NX/np)*rank-1; //DEBUG //fprintf(stdout, "[WORKER %d] Offset: %d | pNX: %d.\n",rank,offset,pNX); // Set initial and boundary conditions initializeArray (X[0], pNX, NX, NY, offset, rank, np); initializeArray (X[1], pNX, NX, NY, offset, rank, np); // setupBoundaryConditions(X[0], NX, NY); // setupBoundaryConditions(X[1], NX, NY); // Inicialitzem aquestes variables aquí segons el tipus de procés per no haver-ho // de fer en totes les iteracions dins de l'updateNonBlocking NYPROB = NY - 1; //initNX i endNX indiquen el rang files a processar en la funció 'updateNonBlocking' //que no depenen d'altres processos (la fila endNX no inclosa) if (rank==MASTER) { initNX = 1; endNX = pNX - 2; } else if (rank==np-1) { initNX = 2; endNX = pNX - 1; } else { initNX = 2; endNX = pNX - 2; } //Amb això ens evitem que peti el programa en casos puntuals on pNX //sigui tan petit que no hi hagi files sense dependències if (initNX>endNX) endNX=initNX; // Main calculations iz = 0; it = 0; //A la 1a iteració no hi ha comunicació en l'update if(verbose) { fprintf(stdout, "[WORKER %d] iteration %ld, time %.3f seconds\n",rank, it, gettime()-start); } update(pNX-1, NYPROB, X[iz], X[1-iz]); iz = 1 - iz; //Resta d'iteracions (update no bloquejant) for (it = 1; it < MAXSTEP; it++) { // Actualitzem amb els valors dels nostres companys workers realitzarComunicacio(rank, np, X[iz], pNX, NY, it, sendRequest, recvRequest); if(verbose && (it%(MAXSTEP/10) == 0)) { fprintf(stdout, "[WORKER %d] iteration %ld, time %.3f seconds\n",rank, it, gettime()-start); } updateNonBlocking(initNX, endNX, NYPROB, X[iz], X[1-iz], rank, np, it, sendRequest, recvRequest); iz = 1 - iz; } // A X[iz] tenim les finals, però separades en els diferents workers. S'ha de tornar a ajuntar tot al master // Creem de nou X[1-iz] amb la mida original, i hi posem les dades del master. Seguidament demanem totes les altres dades // Preparem martiu de la mida original, només cal a la master X[1-iz] = make2DDoubleArray(NX, NY); // Reagrupem. X serà X[iz] (resultats parcials) i X2 X[1-iz], a on quedarà el resultat final reagrupat ajuntarMatriuFinal(rank, np, X[iz], X[1-iz], NX, pNX, NY); // Save output file if(flag && rank==MASTER) save(X[1-iz], NX, NY, file); free2DDoubleArray(X[iz], pNX); free2DDoubleArray(X[1-iz], NX); // End time end = gettime(); // Get information: wall clock time, problem size, ... if (rank==MASTER) { if(verbose) { fprintf(stdout, "[INFO] Convergence after %d steps\n", MAXSTEP); fprintf(stdout, "[INFO] Problem size %d [%dx%d]\n", NY*NX, NX, NY); fprintf(stdout, "[INFO] Wall clock time %lf seconds\n",(end-start)); if(flag) fprintf(stdout, "[INFO] Output file %s\n", file); } else { printf("Time %.3f seconds, Size %d [%dx%d]\n", end - start, NY*NX, NX, NY); } } // Shut down MPI MPI_Finalize(); exit(EXIT_SUCCESS); }
TwoStacks<T>::TwoStacks(int size){ _size = size; initializeArray(); }
TwoStacks<T>::TwoStacks(){ _size = DEFAULT_SIZE; initializeArray(); }