字串轉置

啊…腦袋裡python的語法還是記不得呀…
果然要有一本工具書…一個字串轉置就因為怎麼令一個矩陣來轉置都忘了。

以下參考其他人做法
def reverse(text):
    result = ""
    for idx in range(len(text) - 1, -1, -1):
        result += text[idx] #用於srting
    print result
    return result
def reverse(x):

    y = [ ]
    for n in x: 
        y.insert(0, n) #用於list

    y = "".join(y)   
    return str(y)

print reverse("text")   
 應該還可以使用.append()#用於list,來完成這個工作。

python 數字類別測試func.

1.整數測試: way 1: 利用math 模組測試 import math def is_int(x): if type(x) == int: return True else: return False way 2:利用round(),exp.:round(2.3) → 2 def is_int(x): if x == round(x): return True else: return False way 3:直接演算,取餘數 def is_int(x): if x%1 == 0: return True else: return False

2.質數測試
def is_prime(x):
    if x < 2:
        return False
    for n in range(2, x-1):
        if x % n == 0:
            return False
    else: 

        return True

http://tw.download.nvidia.com/XFree86/Linux-x86_64/310.40/README/installdriver.html


http://tw.download.nvidia.com/XFree86/Linux-x86_64/310.40/README/commonproblems.html#nouveau

http://www.cyberciti.biz/howto/debian-linux/install-nvidia-proprietary-unix-driver/

http://wiki.debian.org/NvidiaGraphicsDrivers#Install_the_kernel_module

http://www.cyberciti.biz/howto/debian-linux/install-nvidia-proprietary-unix-driver/

WMP IN CHROME


瀏覽器Chrome
安裝外掛套件後
沒有效
這時 檢查一下你的D槽或E槽
會有一個資料夾 PFiles\Plugins\np-mswmp.dll
把np-mswmp.dll 更名成NPMSWMP.dll
放到 “C:\Program Files (x86)\Windows Media Player\"下
重開google chrome後即可播放了。

debain 主機名稱修改

嗯..因為老師問我,怎麼把主機名稱"gauss"改成"Gauss"
傻了我@_@,因為我也不知道。
所以就查了一下,要怎麼改主機名稱
debain:
主機名稱是紀錄在
/etc/hostname
裡面,修改之後
/etc/init.d/hostname.sh start
登出再登入就會重新load hostname file了
不然就是重開機囉XP

cuda & openmp

nvcc file.cu -Xcompiler -fopenmp

OpenMP常遇到的問題

最近在學習OpenMP,基本上使用方法大概都可以由前一篇的文章連結學習到
不過小弟我在把我的code改成OpenMP的時候。速度卻比原本的慢了快100倍......
因此來把問題寫下...避免之後遇到不知道怎麼處理
1.rand()
rand()涵數是一個在global memory上執行的涵數,因此如果要在多個threads上同時執行。
rand()會有threads-safe的問題,所以在執行上會只淮許一個threads進去。

目前有兩個解決的方法
i。rand() → rand_r()
   drand48() → drand48_r()

Example: rand_r()

#pragma omp parallel firstprivate(a) num_threads(4)
  {
    unsigned int seed;  //threads_number_n(or other parameters)   
    seed = omp_get_thread_num();

    for(int i = 0;i< 100;i++){
    a = rand_r(&seed); 
      printf("threadnum = %d a = %f\n",omp_get_thread_num(),a);
      }
  } 


Example drand48_r()

#pragma omp parallel firstprivate(a) num_threads(4)
  {
    struct drand48_data drand_buf;
    unsigned short int x=0,y=0,z=0;
    long int see = 0;
    unsigned short int seed16v[3]={x,y,z};

    seed48_r(seed16v,&drand_buf);
    srand48_r(see,&drand_buf);
     //如此一來,四個threads都會是一樣的結果,記得seed48_r與srand48_r都要ini
     //否則結果會讓你很意外?      
    do{   
     
      for(int i = 0;i< 100;i++){
    drand48_r(&drand_buf,&a);     
      printf("threadnum = %d a = %f\n",omp_get_thread_num(),a);
      }   
#pragma omp barrier
#pragma omp single
      {
    txt++;
      }    
   }while(txt < 10);
  } 
參考文章
參考文章2


ii。另外尋找其他parallel random number generators
    → SPRNG  (還不會使用)
    → CUDA   (請查閱user guide)


2.malloc & free allocate (突然覺得我很雖...剛好都遇到了。_。)
其理由也是跟rand很像,他是直接把memory開在global memory上,因些無法直接分配給各個threads (不是很確定我這樣子解理對不對,畢竟對多執行緒還不是很了解)

以下是引述Jim Dempsey部落格裡的範例:
When you want each thread to have their own array
double* array = 0;   // *** bad, pointer in wrong scope
                     // ok to do this when shared(array) on pragma
#pragma omp parallel
{
    array = new double[count];  // *** bad all threads sharing same pointer
                                // *** 2nd and later threads overwrite pointer
   ...
   delete [] array; // *** 2nd and later threads returning same memory
}

------------------------------------

#pragma omp parallel
{
    double* array = 0;
    array = new double[count];  // *** good when you want each thread to have seperate copy
    ...
    delete [] array; // *** good each thread returning seperate copy
}

--------------------

double* array = 0; // OK because of private(array) on pragma
#pragma omp parallel private(array)
{
    array = new double[count];  // *** good when you want each thread to have seperate copy
    ...
    delete [] array; // *** good each thread returning seperate copy
}

--------------------

double* array = 0;
#pragma omp parallel private(array)
{
    array = new double[count];  // *** good when you want each thread to have seperate copy
    ...
}
delete [] array; // *** bad main thread returning one copy

There is nothing wrong with new/delete inside parallel regions, in fact it may be required when you want each thread to have seperate data (e.g. for temporary arrays).

原文連結