fork download
  1. #include <iostream>
  2. #include <pthread.h>
  3. #include <functional>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. #include "Timer.h"
  12.  
  13. typedef std::vector<double> vector_t;
  14. typedef vector_t::iterator iter_t;
  15. typedef iter_t::value_type value_t;
  16.  
  17. const int NUM_THREADS = 4;
  18.  
  19. pthread_t threads[NUM_THREADS];
  20. int n_cpus=1;
  21.  
  22. struct arg_t{
  23. iter_t begin;
  24. iter_t end;
  25. cpu_set_t cpuset;
  26. };
  27. struct fubar{};
  28.  
  29. void* sort_wrap(void* arg){
  30. iter_t begin = ((arg_t*)arg)->begin;
  31. iter_t end = ((arg_t*)arg)->end;
  32. cpu_set_t cpuset = ((arg_t*)arg)->cpuset;
  33. sched_setaffinity( 0, sizeof(cpuset), &cpuset);
  34. std::sort(begin, end);
  35. pthread_exit(0);
  36. return 0;
  37. }
  38.  
  39. int pt_sort(iter_t begin, iter_t end) {
  40. cpu_set_t cpu_set_zero;
  41. CPU_ZERO(&cpu_set_zero);
  42. for(int j = 0; j< n_cpus; j++){
  43. CPU_SET( j , &cpu_set_zero);
  44. }
  45. pthread_attr_t attr;
  46. pthread_attr_init(&attr);
  47. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  48.  
  49. iter_t mid = std::partition(begin, end - 1, std::bind1st(less<value_t > (), *(end - 1)));
  50. swap(*mid, *(end - 1));
  51. cout<< (mid-end)<<endl;
  52.  
  53. iter_t end_low = mid;
  54. iter_t mid_low = std::partition(begin, end_low-1, std::bind1st(less<value_t > (), *(end_low-1 - 1)));
  55. swap(*mid_low, *(end_low - 1));
  56. cout<< (mid_low-end_low)<<endl;
  57.  
  58. iter_t begin_high = mid+1;
  59. iter_t mid_high = std::partition(begin_high, end-1, std::bind1st(less<value_t > (), *(end-1 - 1)));
  60. swap(*mid_high, *(end - 1));
  61. cout<< (mid_high-end)<<endl;
  62.  
  63. // 4 parts to thread
  64. cpu_set_t cpu0 = cpu_set_zero;
  65. CPU_SET(0 % n_cpus, &cpu0 );
  66. cpu_set_t cpu1 = cpu_set_zero;
  67. CPU_SET(1 % n_cpus, &cpu1 );
  68. cpu_set_t cpu2 = cpu_set_zero;
  69. CPU_SET(2 % n_cpus, &cpu2 );
  70. cpu_set_t cpu3 = cpu_set_zero;
  71. CPU_SET(3 % n_cpus, &cpu3 );
  72.  
  73. arg_t a1={begin, mid_low, cpu0};
  74. arg_t a2={mid_low+1, end_low, cpu1};
  75. arg_t a3={begin_high, mid_high, cpu2};
  76. arg_t a4={mid_high+1, end, cpu3};
  77.  
  78. int rc1 = pthread_create(&threads[0], &attr, sort_wrap, (void *) &a1);
  79. if ( rc1 ) throw fubar();
  80. int rc2 = pthread_create(&threads[1], &attr, sort_wrap, (void *) &a2);
  81. if ( rc2 ) throw fubar();
  82. int rc3 = pthread_create(&threads[2], &attr, sort_wrap, (void *) &a3);
  83. if ( rc3 ) throw fubar();
  84. int rc4 = pthread_create(&threads[3], &attr, sort_wrap, (void *) &a4);
  85. if ( rc4 ) throw fubar();
  86.  
  87.  
  88. for(int i_threads = 0; i_threads<4; i_threads++){
  89. pthread_join(threads[i_threads], 0);
  90. }
  91.  
  92. return 0;
  93. }
  94.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:11:19: fatal error: Timer.h: No such file or directory
compilation terminated.
stdout
Standard output is empty