+ //each process gets where it should start and stop in the file
+ if (output != "square") {
+ start = int (sqrt(float(pid)/float(processors)) * numSeqs);
+ end = int (sqrt(float(pid+1)/float(processors)) * numSeqs);
+ }else{
+ start = int ((float(pid)/float(processors)) * numSeqs);
+ end = int ((float(pid+1)/float(processors)) * numSeqs);
+ }
+
+ if (output == "column") {
+ MPI_File outMPI;
+ int amode=MPI_MODE_CREATE|MPI_MODE_WRONLY;
+
+ //char* filename = new char[outputFile.length()];
+ //memcpy(filename, outputFile.c_str(), outputFile.length());
+
+ char filename[1024];
+ strcpy(filename, outputFile.c_str());
+
+ MPI_File_open(MPI_COMM_WORLD, filename, amode, MPI_INFO_NULL, &outMPI);
+ //delete filename;
+
+ if (pid == 0) { //you are the root process
+
+ //do your part
+ string outputMyPart;
+
+ driverMPI(start, end, outMPI, cutoff);
+
+ if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); return 0; }
+
+ //wait on chidren
+ for(int i = 1; i < processors; i++) {
+ if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); return 0; }
+
+ char buf[5];
+ MPI_Recv(buf, 5, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
+ }
+ }else { //you are a child process
+ //do your part
+ driverMPI(start, end, outMPI, cutoff);
+
+ if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); return 0; }
+
+ char buf[5];
+ strcpy(buf, "done");
+ //tell parent you are done.
+ MPI_Send(buf, 5, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
+ }
+
+ MPI_File_close(&outMPI);
+
+ }else { //lower triangle format
+ if (pid == 0) { //you are the root process
+
+ //do your part
+ string outputMyPart;
+ unsigned long long mySize;
+
+ if (output != "square"){ driverMPI(start, end, outputFile, mySize); }
+ else { driverMPI(start, end, outputFile, mySize, output); }
+
+ if (m->control_pressed) { outputTypes.clear(); return 0; }
+
+ int amode=MPI_MODE_APPEND|MPI_MODE_WRONLY|MPI_MODE_CREATE; //
+ MPI_File outMPI;
+ MPI_File inMPI;
+
+ //char* filename = new char[outputFile.length()];
+ //memcpy(filename, outputFile.c_str(), outputFile.length());
+
+ char filename[1024];
+ strcpy(filename, outputFile.c_str());
+
+ MPI_File_open(MPI_COMM_SELF, filename, amode, MPI_INFO_NULL, &outMPI);
+ //delete filename;
+
+ //wait on chidren
+ for(int b = 1; b < processors; b++) {
+ unsigned long long fileSize;
+
+ if (m->control_pressed) { outputTypes.clear(); MPI_File_close(&outMPI); return 0; }
+
+ MPI_Recv(&fileSize, 1, MPI_LONG, b, tag, MPI_COMM_WORLD, &status);
+
+ string outTemp = outputFile + toString(b) + ".temp";
+
+ char* buf = new char[outTemp.length()];
+ memcpy(buf, outTemp.c_str(), outTemp.length());
+
+ MPI_File_open(MPI_COMM_SELF, buf, MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_RDONLY, MPI_INFO_NULL, &inMPI);
+ delete buf;
+
+ int count = 0;
+ while (count < fileSize) {
+ char buf2[1];
+ MPI_File_read(inMPI, buf2, 1, MPI_CHAR, &status);
+ MPI_File_write(outMPI, buf2, 1, MPI_CHAR, &status);
+ count += 1;
+ }
+
+ MPI_File_close(&inMPI); //deleted on close
+ }
+
+ MPI_File_close(&outMPI);
+ }else { //you are a child process
+ //do your part
+ unsigned long long size;
+ if (output != "square"){ driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size); }
+ else { driverMPI(start, end, (outputFile + toString(pid) + ".temp"), size, output); }
+
+ if (m->control_pressed) { return 0; }
+
+ //tell parent you are done.
+ MPI_Send(&size, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD);
+ }
+ }
+ MPI_Barrier(MPI_COMM_WORLD); //make everyone wait - just in case
+#else