Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. /*
  2. Josh Stone
  3. CS 476
  4. Lab 4
  5. Dr. Zoppetti
  6. /*
  7.  
  8. /**************************************************************************/
  9. import std.stdio : writeln;
  10. import std.range : iota;
  11. import std.parallelism : parallel;
  12. import std.algorithm, std.array, std.range, std.random, std.algorithm, core.thread, std.stdio;
  13. import std.datetime;
  14. /**************************************************************************/
  15. //http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#D
  16. T[]
  17. mergeSorted(T)(in T[] D) {
  18. if (D.length < 2)
  19. return D.dup;
  20. return [D[0 .. $ / 2].mergeSorted, D[$ / 2 .. $].mergeSorted].nWayUnion.array;
  21. }
  22. /**************************************************************************/
  23. void
  24. ToString(int[] arr)
  25. {
  26. write("A[] = [");
  27. int i = 0;
  28. if (arr.length == 0)
  29. {
  30. writeln("]");
  31. }
  32. while (i < arr.length)
  33. {
  34. write(arr[i]);
  35. ++i;
  36. if (i == arr.length)
  37. {
  38. writeln("]");
  39. break;
  40. }
  41. else
  42. {
  43. write(", ");
  44. }
  45. }
  46. }
  47. /**************************************************************************/
  48. void
  49. parallelMergeSort (int [] array, int threads, int N)
  50. {
  51. int [][] result;
  52. int numElemsPerThread = N / threads;
  53. int numTougherThreads = N % threads;
  54.  
  55. ulong lbound = 0;
  56. ulong ubound = 0;
  57. foreach (i; iota(threads).parallel) {
  58. // The body of the foreach loop is executed in parallel for each i
  59. // writeln("core ", i, " processing ");
  60. if (i < numTougherThreads)
  61. {
  62. lbound = i*(numElemsPerThread+1);
  63. ubound = i*(numElemsPerThread+1)+(numElemsPerThread+1);
  64.  
  65. if ((ubound <= N) && (lbound <= N))
  66. {
  67. result ~= array[lbound..ubound].mergeSorted;
  68. }
  69. }
  70.  
  71. if (i >= numTougherThreads)
  72. {
  73. int scale = 0;
  74. if (numTougherThreads != 0)
  75. {
  76. scale = numTougherThreads;
  77. }
  78. lbound = i*numElemsPerThread+scale;
  79. ubound = i*numElemsPerThread+scale+numElemsPerThread;
  80.  
  81. if ((ubound <= N) && (lbound <= N))
  82. {
  83. result ~= array[lbound..ubound].mergeSorted;
  84. }
  85. }
  86. }
  87. nWayUnion(result);
  88. // writeln("Parallel MergeSort: ", nWayUnion(result));
  89. //writeln("");
  90. }
  91. /**************************************************************************/
  92.  
  93. void main()
  94. {
  95. //N >= 0
  96. write("Enter size N >= 0 ==> ");
  97. int N;
  98. readf(" %s", &N);
  99. //Threads > 0
  100. write("Enter threads > 0 ==> ");
  101. int threads;
  102. readf(" %s", &threads);
  103.  
  104. //writeln("Threads = ", threads);
  105. //writeln("Size = ", N);
  106. int[] array;
  107. int counter;
  108.  
  109. while (counter < N)
  110. {
  111. auto i = uniform(0, 100);
  112. array ~= i;
  113. // writeln("A[", counter, "]", " = ", array[counter]);
  114. ++counter;
  115. }
  116. // write("Original ");
  117. // array.ToString;
  118. // writeln("");
  119.  
  120. array.parallelMergeSort(threads, N);
  121.  
  122. // write("Serial MergeSort: ");
  123.  
  124. //array.mergeSorted;
  125. return;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement