2012-04-28 195 views
4

我在SIMD中編寫了矩陣乘法的代碼,我可以在Visual Studio中進行編譯,但現在我需要使用gcc/g ++在Ubuntu中進行編譯。如何使用gcc編譯SIMD代碼

我應該使用哪些命令來編譯?我是否需要對代碼本身進行任何更改?

#include <stdio.h> 
#include <stdlib.h> 
#include <xmmintrin.h> 
#include <iostream> 
#include <conio.h> 
#include <math.h> 
#include <ctime> 

using namespace std; 

#define MAX_NUM 1000 
#define MAX_DIM 252 

int main() 
{ 
    int l = MAX_DIM, m = MAX_DIM, n = MAX_DIM; 
    __declspec(align(16)) float a[MAX_DIM][MAX_DIM], b[MAX_DIM][MAX_DIM],c[MAX_DIM][MAX_DIM],d[MAX_DIM][MAX_DIM]; 

    srand((unsigned)time(0)); 

    for(int i = 0; i < l; ++i) 
    { 
     for(int j = 0; j < m; ++j) 
     { 
      a[i][j] = rand()%MAX_NUM; 
     } 
    } 

    for(int i = 0; i < m; ++i) 
    { 
     for(int j = 0; j < n; ++j) 
     { 
      b[i][j] = rand()%MAX_NUM; 
     } 
    } 

    clock_t Time1 = clock(); 

    for(int i = 0; i < m; ++i) 
    { 
     for(int j = 0; j < n; ++j) 
     { 
      d[i][j] = b[j][i]; 
     } 
    } 

    for(int i = 0; i < l; ++i) 
    { 
     for(int j = 0; j < n; ++j) 
     { 
      __m128 *m3 = (__m128*)a[i]; 
      __m128 *m4 = (__m128*)d[j]; 
      float* res; 
      c[i][j] = 0; 
      for(int k = 0; k < m; k += 4) 
      { 
       __m128 m5 = _mm_mul_ps(*m3,*m4); 
       res = (float*)&m5; 
       c[i][j] += res[0]+res[1]+res[2]+res[3]; 
       m3++; 
       m4++; 
      } 
     } 
     //cout<<endl; 
    } 

    clock_t Time2 = clock(); 
    double TotalTime = ((double)Time2 - (double)Time1)/CLOCKS_PER_SEC; 
    cout<<"Time taken by SIMD implmentation is "<<TotalTime<<"s\n"; 

    Time1 = clock(); 

    for(int i = 0; i < l; ++i) 
    { 
     for(int j = 0; j < n; ++j) 
     { 
      c[i][j] = 0; 
      for(int k = 0; k < m; k += 4) 
      { 
       c[i][j] += a[i][k] * b[k][j]; 
       c[i][j] += a[i][k+1] * b[k+1][j]; 
       c[i][j] += a[i][k+2] * b[k+2][j]; 
       c[i][j] += a[i][k+3] * b[k+3][j]; 

      } 
     } 
    } 

    Time2 = clock(); 
    TotalTime = ((double)Time2 - (double)Time1)/CLOCKS_PER_SEC; 
    cout<<"Time taken by normal implmentation is "<<TotalTime<<"s\n"; 

    getch(); 
    return 0; 
} 
+0

我的問題是如何編譯在Ubuntu使用g ++這個代碼或gcc編譯器...我成功了,並在Windows中編譯它與視覺工作室,但不知道如何編譯在Ubuntu .. – abhinav 2012-04-28 19:17:20

回答

7

您需要啓用SSE,

$ g++ -msse3 -O3 -Wall -lrt foo.cpp -o foo 

你也需要改變:

declspec(align(16)) 

這是Windows專用,到更輕便:

__attribute__ ((aligned(16))) 
+0

它給這樣的許多錯誤 // matrix_simd.cpp:25:錯誤:'a'未在此範圍內聲明。 – abhinav 2012-04-28 19:21:09

+0

但它成功編譯,我能夠在Windows中的視覺工作室運行..我得到這個錯誤,我需要在這裏添加任何標題? – abhinav 2012-04-28 19:22:10

+0

查看上面的編輯 – 2012-04-28 19:24:24