Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <vector>
  6. #include <map>
  7. #include <math.h>
  8. #include <mpi.h>
  9.  
  10. using namespace std;
  11.  
  12. char* res_part_file = "./PartialDataSet/residences.dat";
  13. char* serv_part_file = "./PartialDataSet/services.dat";
  14. char* res_full_file = "./FullDataSet/residences.dat";
  15. char* serv_full_file = "./FullDataSet/services.dat";
  16.  
  17. double calculateDistances(vector<map<string, double>> serv, double rX, double rY)
  18. {
  19. map<string, double> closest = map<string, double>();
  20. double d = DBL_MAX;
  21. const double mToKm = 1000.00;
  22. for(vector<map<string, double>>::iterator it = serv.begin(); it != serv.end(); it++){
  23. double distance = 0.0;
  24. distance = sqrt(pow(it->find("xLoc")->second - rX, 2) + pow(it->find("yLoc")->second - rY, 2));
  25. if(distance < d)
  26. d = distance;
  27. }
  28. return d/mToKm;
  29. }
  30.  
  31. map<string, int> analyzeResidents(vector<map<string, double>> params, int rank, int numProcs) {
  32. string line;
  33. unsigned int i_ID;
  34. int i = 0;
  35. char xLoc[13];
  36. char yLoc[14];
  37. map<string, int> residents = map<string, int>();
  38. char* file = res_part_file;
  39. ifstream resFile(file);
  40.  
  41. if (!resFile.is_open() || resFile.fail())
  42. {
  43. cerr << "ERROR (process " << rank << "): Could not open file '"
  44. << file << "'" << endl;
  45. }
  46. else
  47. {
  48. string rec;
  49. getline(resFile, rec);
  50. int rowBytes = resFile.tellg();
  51.  
  52. resFile.seekg(0, ios::end);
  53. int fBytes = (int) resFile.tellg();
  54. int numRows = fBytes/rowBytes;
  55. int numRowsPerProc = numRows/numProcs;
  56.  
  57. resFile.seekg(rank * numRowsPerProc * rowBytes, ios::beg);
  58. int numRowsMine = numRowsPerProc;
  59. if(rank == numProcs - 1)
  60. numRowsMine += numRows % numProcs;
  61.  
  62. for (int i = 0; i < numRowsMine; ++i)
  63. {
  64. resFile >> xLoc >> yLoc;
  65.  
  66. if (resFile.eof())
  67. break;
  68.  
  69. double d = calculateDistances(params, stod(xLoc), stod(yLoc));
  70. if(d <= 1.0)
  71. residents["0-1"]++;
  72. else if(d > 1.0 && d <= 2.0)
  73. residents["1-2"]++;
  74. else if(d > 2.0 && d <= 5.0)
  75. residents["2-5"]++;
  76. else
  77. residents[">5"]++;
  78. }
  79. //resFile.close();
  80. }
  81. return residents;
  82. }
  83.  
  84. //vector<map<string, double>> analyzeServices(unsigned int params, int rank, int numProcs) {
  85. // string line;
  86. // unsigned int i_ID;
  87. // int i = 0;
  88. // char id[7];
  89. // char xLoc[13];
  90. // char yLoc[14];
  91. // vector<map<string, double>> services = vector<map<string, double>>();
  92. // char* file = serv_part_file;
  93. // ifstream servFile(file);
  94. //
  95. // if (!servFile.is_open() || servFile.fail())
  96. // {
  97. // cerr << "ERROR (process " << rank << "): Could not open file '"
  98. // << file << "'" << endl;
  99. // }
  100. // else
  101. // {
  102. // //Determine number of bytes per record
  103. // string rec;
  104. // getline(servFile, rec);
  105. // int rowBytes = (int)servFile.tellg();
  106. //
  107. // servFile.seekg(0, ios::end);
  108. // int fBytes = (int) servFile.tellg();
  109. // int numRows = fBytes/rowBytes;
  110. // int numRowsPerProc = numRows/numProcs;
  111. //
  112. // servFile.seekg(rank * numRowsPerProc * rowBytes, ios::beg);
  113. // int numRowsMine = numRowsPerProc;
  114. // if(rank == numProcs - 1)
  115. // numRowsMine += numRows % numProcs;
  116. //
  117. // for (int i = 0; i < numRowsMine; ++i)
  118. // {
  119. // servFile >> id >> xLoc >> yLoc;
  120. //
  121. // if (servFile.eof())
  122. // break;
  123. //
  124. // i_ID = (unsigned int) stoi(id);
  125. // if(i_ID == params){
  126. // i++;
  127. // map<string, double> serv = map<string, double>();
  128. // serv.insert(pair<string, double>("id", stod(id)));
  129. // serv.insert(pair<string, double>("xLoc", stod(xLoc)));
  130. // serv.insert(pair<string, double>("yLoc", stod(yLoc)));
  131. // services.push_back(serv);
  132. // }
  133. // }
  134. //
  135. // //servFile.close();
  136. // memset(&id, 0, sizeof(id));
  137. // memset(&xLoc, 0, sizeof(xLoc));
  138. // memset(&yLoc, 0, sizeof(yLoc));
  139. // }
  140. // return services;
  141. //}
  142.  
  143. vector<map<string, double>> analyzeServices(unsigned int params) {
  144. string line;
  145. unsigned int i_ID;
  146. int i = 0;
  147. char id[7];
  148. char xLoc[13];
  149. char yLoc[14];
  150. vector<map<string, double>> services = vector<map<string, double>>();
  151. ifstream servFile("./FullDataSet/services.dat");
  152.  
  153. while(getline(servFile, line)){
  154. istringstream iss(line);
  155. iss >> id;
  156. iss >> xLoc;
  157. iss >> yLoc;
  158. i_ID = (unsigned int) stoi(id);
  159. if(i_ID == params){
  160. i++;
  161. //cout << id << " : " << xLoc << " : " << yLoc << endl;
  162. map<string, double> serv = map<string, double>();
  163. serv.insert(pair<string, double>("id", stod(id)));
  164. serv.insert(pair<string, double>("xLoc", stod(xLoc)));
  165. serv.insert(pair<string, double>("yLoc", stod(yLoc)));
  166. services.push_back(serv);
  167. }
  168. }
  169. servFile.close();
  170. memset(&id, 0, sizeof(id));
  171. memset(&xLoc, 0, sizeof(xLoc));
  172. memset(&yLoc, 0, sizeof(yLoc));
  173.  
  174. return services;
  175. }
  176.  
  177. double CalcFrequency(int occurances, int numHands){
  178. double p = 100.00;
  179. double num = double(numHands);
  180. return (occurances / num) * p;
  181. }
  182.  
  183. void displayOutput(map<string, int> res, int numProcs, double elapsedTime, int rank, int serviceCount, int serviceNum){
  184. map<string, int> total = map<string,int>();
  185.  
  186. MPI_Reduce(&res["0-1"], &total["0-1"], 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  187. MPI_Reduce(&res["1-2"], &total["1-2"], 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  188. MPI_Reduce(&res["2-5"], &total["2-5"], 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  189. MPI_Reduce(&res[">5"], &total[">5"], 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
  190.  
  191. int msgBuff[100];
  192. static MPI_Status status;
  193.  
  194. if(rank > 0)
  195. {
  196. int totalres = res["0-1"] + res["1-2"] + res["2-5"] + res[">5"];
  197. MPI_Recv(&msgBuff, 1, MPI_INT, MPI_ANY_SOURCE, 1, MPI_COMM_WORLD, &status); // arbitrary hold
  198. cout << endl << "Process #" << rank + 1 << " Results for " << totalres << " Addresses..." << endl;
  199. cout << "Nearest Service (km) # of Addresses % of Addresses" << endl;
  200. //printf("%20s %14s %14s\n", "Nearest Service (km)", "# of Addresses", " % of Addresses");
  201. for(map<string, int>::iterator it = res.begin(); it != res.end(); it++){
  202. //printf("%20s %14s %14s\n", it->first, it->second, CalcFrequency(it->second, totalres));
  203. cout << it->first << " : " << it->second << " : " << CalcFrequency(it->second, totalres) << endl;
  204. }
  205.  
  206. cout << endl;
  207.  
  208. if(numProcs - 1 != rank)
  209. MPI_Send(&msgBuff[0], 1, MPI_INT, rank + 1, 1, MPI_COMM_WORLD); //tell each slave in order to finish
  210. else
  211. MPI_Send(&msgBuff[0], 1, MPI_INT, 0, 2, MPI_COMM_WORLD); // tell master im done
  212. }
  213. if(rank == 0)
  214. {
  215. int totalres = total["0-1"] + total["1-2"] + total["2-5"] + total[">5"];
  216. printf("%28s%16s\n", "Service:", "PLACEHOLDER");
  217. printf("%28s%16d\n", "Service Code:", serviceNum);
  218. printf("%28s%16d\n", "Number of Service Locations:", serviceCount);
  219. printf("%28s%16d\n", "Number of Processes:", numProcs);
  220. printf("%28s%16g\n", "Elapsed Time in Seconds:", elapsedTime);
  221. cout << endl;
  222.  
  223. cout << "Process #" << rank + 1 << " Results for " << totalres / numProcs << " Addresses..." << endl;
  224. //printf("%20s %14s %14s\n", "Nearest Service (km)", "# of Addresses", " % of Addresses");
  225. cout << "Nearest Service (km) # of Addresses % of Addresses" << endl;
  226. for(map<string, int>::iterator it = res.begin(); it != res.end(); it++){
  227. //printf("%20s %14s %14s\n", it->first, it->second, CalcFrequency(it->second, totalres));
  228. cout << it->first << " : " << it->second << " : " << CalcFrequency(it->second, totalres) << endl;
  229. }
  230.  
  231. MPI_Send(&msgBuff[0], 1, MPI_INT, 1, 1, MPI_COMM_WORLD); // start other threads
  232. MPI_Recv(&msgBuff, 1, MPI_INT, MPI_ANY_SOURCE, 2, MPI_COMM_WORLD, &status); // arbitrary hold
  233.  
  234. //aggregate results
  235. cout << endl;
  236.  
  237. cout << "Aggregate Results for all " << totalres << " Addresses..." << endl;
  238. cout << "Nearest Service (km) # of Addresses % of Addresses" << endl;
  239. for(map<string, int>::iterator it = total.begin(); it != total.end(); it++){
  240. cout << it->first << " : " << it->second << " : " << CalcFrequency(it->second, totalres) << endl;
  241. }
  242. }
  243. }
  244.  
  245. int main(int argc, char* argv[]){
  246. if(MPI_Init(&argc, &argv) == MPI_SUCCESS){
  247. // Get the # of processes and rank of this process
  248. int numProcs, rank;
  249. MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
  250. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  251.  
  252. if(argc == 2) {
  253. int params = stoi(argv[1]);
  254. if(params > 0) {
  255. double startTime = MPI_Wtime();
  256. if (rank == 0)
  257. cout << "Proximities of Residential Addresses to Services in Toronto" << endl << string(59, '-') << endl;
  258.  
  259. vector<map<string, double>> services = analyzeServices(params);
  260. map<string, int> resCount = analyzeResidents(services, rank, numProcs);
  261.  
  262. double elapsedTime = MPI_Wtime() - startTime;
  263. displayOutput(resCount, numProcs, elapsedTime, rank, services.size(), params);
  264. } else {
  265. cout << "Parameter must be positive integer" << endl;
  266. return EXIT_FAILURE;
  267. }
  268. } else if (rank == 0) {
  269. cout << "Invalid Arguments" << endl;
  270. return EXIT_FAILURE;
  271. }
  272. MPI_Finalize();
  273. }
  274. return EXIT_SUCCESS;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement