cs205-lecture-examples

Example codes used during Harvard CS205 lectures
git clone https://git.0xfab.ch/cs205-lecture-examples.git
Log | Files | Refs | README | LICENSE

affinity_spread.cpp (861B)


      1 #include "helper.h"
      2 #include <iostream>
      3 #include <omp.h>
      4 #include <sstream>
      5 #include <vector>
      6 
      7 int main(void)
      8 {
      9     std::vector<std::string> ordered_output;
     10 
     11 #pragma omp parallel proc_bind(spread)
     12     {
     13         const int tid = omp_get_thread_num();
     14         const int nthreads = omp_get_num_threads();
     15 #pragma omp single
     16         ordered_output.resize(nthreads);
     17 
     18         int cpuid, nodeid;
     19         tacc_rdtscp(&cpuid, &nodeid);
     20 
     21         const std::string hostname = gethostname();
     22 
     23         std::ostringstream mystream;
     24         mystream << "[thread=" << tid << "/" << nthreads << "]\t"
     25                  << "Running on host " << hostname.c_str() << "\tCPU " << cpuid
     26                  << "\tNUMA NODE " << nodeid << '\n';
     27         ordered_output[tid] = mystream.str();
     28     }
     29 
     30     for (const auto &s : ordered_output) {
     31         std::cout << s;
     32     }
     33 
     34     return 0;
     35 }