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 }

 

原文地址:http://www.cnblogs.com/cofludy/p/16853482.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性