首页 > 编程语言 >cpp 并行编程 pthread 框架 绑定核心运行

cpp 并行编程 pthread 框架 绑定核心运行

时间:2022-11-03 10:14:20浏览次数:54  
标签:thread int 绑定 param pthread cpp include pram

  1 #include <stdio.h>
  2 #include <math.h>
  3 #include <pthread.h>
  4 #include<stdio.h>
  5 #include<iostream>
  6 #include<stdlib.h>
  7 #include<time.h>
  8 
  9 #include "Atomic_data.h"
 10 #include "mutex_microOMP.h"
 11 
 12 
 13 
 14 
 15 extern void  run_sim_OMP(const int coreNum, MCF_MTX_ATOM* pstAtomic, int * mutex, 
 16                             int thread_cnt, bool* sense, gomp_barrier_t* barPst);
 17 
 18 
 19 typedef struct tag_pass_pram_to_subthread{
 20     int coreNum;
 21     int *mutex;
 22     int thread_cnt;
 23     bool *sense;
 24     MCF_MTX_ATOM* pstAtomic; 
 25     gomp_barrier_t* barPst;
 26 }pass_pram_to_subthread __attribute__((aligned(128)));
 27 
 28 
 29 cpu_set_t cpuset,cpuget;
 30 
 31 double waste_time(long n){
 32     double res = 0;
 33     long i = 0;
 34     while (i <n) {
 35         i++;
 36         res += sqrt(i);
 37     }
 38     return res;
 39 }
 40 
 41 
 42 void runtest(pass_pram_to_subthread* this_param){
 43 
 44     waste_time(200);
 45 
 46     run_sim_OMP(this_param->coreNum , this_param->pstAtomic, this_param->mutex, 
 47                 this_param->thread_cnt, this_param->sense, this_param->barPst);
 48 
 49 
 50 }
 51 
 52 
 53 void *thread_func(void *param) {
 54     pass_pram_to_subthread* this_param = (pass_pram_to_subthread*) param;
 55     int CPU_number = this_param->coreNum;
 56     printf("Core %d is running!\n", CPU_number);
 57 
 58     CPU_ZERO(&cpuset);
 59     CPU_SET(CPU_number,  &cpuset); /* cpu 0 is in cpuset now */
 60     /* bind process to processor 0 */
 61     if (pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) !=0) {
 62         perror("pthread_setaffinity_np");
 63     }
 64 
 65     /* waste some time so the work is visible with "top" */
 66     runtest(this_param);
 67 
 68     //printf("result: %f\n", waste_time(5));
 69     pthread_exit(NULL);
 70 }
 71 
 72 
 73 int main(int argc, char *argv[]) { 
 74     const int thread_num=10;
 75 
 76     pthread_t my_thread[thread_num];
 77     int coreNum[thread_num];
 78 
 79     time_t startwtime, endwtime;
 80     startwtime = time (NULL); 
 81     
 82     MCF_MTX_ATOM *pst = new MCF_MTX_ATOM;
 83     pst->lCounter=0;
 84 
 85     pass_pram_to_subthread *param = new pass_pram_to_subthread[thread_num]; 
 86 
 87     int loc_mutex=0; 
 88     bool global_sense=false; 
 89 
 90     gomp_barrier_t* global_barPst = new gomp_barrier_t;
 91     gomp_barrier_init(global_barPst, thread_num);
 92 
 93     for(int i=0; i<thread_num; ++i){
 94         coreNum[i] = i;
 95         param[i].coreNum=i;
 96         param[i].thread_cnt=thread_num;
 97         param[i].pstAtomic = pst;
 98         param[i].sense = &global_sense;
 99         param[i].mutex=&loc_mutex;
100         param[i].barPst = global_barPst;
101     }
102 
103     for(int i=0; i<thread_num; ++i){
104         if (pthread_create(my_thread+i, NULL, thread_func, (void *)&param[i]) != 0) {
105             perror("pthread_create");
106         }
107     }
108 
109     for(int i=0; i<thread_num; ++i){
110         pthread_join(my_thread[i],NULL);
111     }
112 
113     endwtime = time (NULL);
114     printf ("\n \n final result=%d, loc_mutex= %d, clock time = %d\n", pst->lCounter, loc_mutex, (endwtime - startwtime));
115 
116     delete pst;
117     delete[] param;
118 
119     return 0;
120 }

 

标签:thread,int,绑定,param,pthread,cpp,include,pram
From: https://www.cnblogs.com/cofludy/p/16853482.html

相关文章