Parrot源码分析之海贼王

本文深入分析Parrot源码,研究speedup-example程序在使用Parrot后性能提升的原因。作者指出,减少Context Switch和降低锁竞争是关键因素,同时探讨了线程创建与执行顺序对性能的影响。通过对源码的逐步解析,揭示了Parrot如何通过调度策略减少锁竞争,从而提高性能。
摘要由CSDN通过智能技术生成

我们的目的是找到speedup-example在使用Parrot加速的原因,如果只说它源于Context Switch的减少,有点简单了,它到底为什么减少了?除了Context Switch外是否还有其他的performance counter也对提速有帮助?这些都是值得去思考的问题。先来看一下我们用来探索Parrot奥秘的程序speedup-example.cpp。


前言:RRScheduler::getTurn&RRScheduler::wait_t::wait


speedup-example.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* Copyright (c) 2013,  Regents of the Columbia University  
  2.  * All rights reserved. 
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 
  5.  * 
  6.  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
  7.  * 
  8.  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other  
  9.  * materials provided with the distribution. 
  10.  * 
  11.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  12.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR  
  13.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  14.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  15.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  16.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  17.  */  
  18.   
  19. #include <pthread.h>  
  20. #include <stdio.h>  
  21. #include <stdlib.h>  
  22. #include <errno.h>  
  23. #include <assert.h>  
  24. //#include "tern/user.h"  
  25.   
  26. #define N 4  
  27. //#define M 30000  
  28. #define M 30000*10  
  29.   
  30. //int nwait = 0;  
  31. volatile long long sum;  
  32. volatile long long sum_2;  
  33. long loops = 6e3;  
  34. pthread_mutex_t mutex;  
  35. pthread_cond_t cond;  
  36. pthread_barrier_t bar;  
  37.   
  38. void set_affinity(int core_id) {  
  39.   cpu_set_t cpuset;  
  40.   CPU_ZERO(&cpuset);  
  41.   CPU_SET(core_id, &cpuset);  
  42.   assert(pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) == 0);  
  43. }  
  44.   
  45. void* thread_func(void *arg) {  
  46.   set_affinity((int)(long)arg);  
  47.   for (int j = 0; j < M; j++) {  
  48.     //pthread_mutex_lock(&mutex);  
  49.     //nwait++;  
  50.     for (long i = 0; i < loops; i++) // This is the key of speedup for parrot: the mutex needs to be a little bit congested.  
  51.     {    
  52.     sum += i;  
  53.     sum_2 += i;  
  54.     }  
  55.     //pthread_cond_wait(&cond, &mutex);  
  56.   
  57.     //printf("being in the lock is: %lu\n", pthread_self());  
  58.   
  59.     //pthread_mutex_unlock(&mutex);  
  60.     //soba_wait(0);  
  61.     //pthread_barrier_wait(&bar);  
  62.     for (long i = 0; i < loops; i++)  
  63.     {     
  64.     sum += i*i*i*i*i*i;  
  65.     }  
  66.     //fprintf(stderr, "compute thread %u %d\n", (unsigned)thread, sched_getcpu());  
  67.   }  
  68. }  
  69.   
  70. int main(int argc, char *argv[]) {  
  71.   set_affinity(23);  
  72.   //soba_init(0, N, 20);  
  73.   pthread_t th[N];  
  74.   int ret;  
  75.   //pthread_cond_init(&cond, NULL);  
  76.   //pthread_barrier_init(&bar, NULL, N);  
  77.   
  78.   for(unsigned i=0; i<N; ++i) {  
  79.     ret  = pthread_create(&th[i], NULL, thread_func, (void*)i);  
  80.     assert(!ret && "pthread_create() failed!");  
  81.   }  
  82.   
  83.   /*for (int j = 0; j < M; j++) {  
  84.     while (nwait < N) {  
  85.       sched_yield(); 
  86.     } 
  87.     pthread_mutex_lock(&mutex); 
  88.     nwait = 0; 
  89.     //fprintf(stderr, "broadcast %u %d\n", (unsigned)pthread_self(), sched_getcpu()); 
  90.     pthread_cond_broadcast(&cond); 
  91.     pthread_mutex_unlock(&mutex); 
  92.   } 
  93.   */      
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值