2011-12-14 96 views
0

我从网上找到了一个Pthread程序,我想在Visual Studio 2010中运行它,但是我不知道如何在visual studio中使用pthread。以下是我找到的程序:Visual Studio 2010中的pthread(POSIX线程)

#include <stdio.h> 
#include <stdlib.h> 

#define MAX_SIZE 4096 
#define NO_PROCESS 8 

typedef double matrix[MAX_SIZE][MAX_SIZE]; 

int N;   /* matrix size  */ 
int maxnum;  /* max number of element*/ 
char *Init;  /* matrix init type */ 
int PRINT;  /* print switch  */ 
matrix A;  /* matrix A  */ 
double b[MAX_SIZE]; /* vector b */ 
double y[MAX_SIZE]; /* vector y */ 
pthread_barrier_t barrier; 

/* forward declarations */ 
void work(void*); 
void Init_Matrix(void); 
void Print_Matrix(void); 
void Init_Default(void); 
int Read_Options(int, char **); 

int main(int argc, char **argv) 
{ 
    pthread_t threads[NO_PROCESS]; 
    int timestart, timeend, iter; 
    long i; 

    Init_Default();  /* Init default values */ 
    Read_Options(argc, argv); /* Read arguments */ 
    Init_Matrix();  /* Init the matrix */ 

    pthread_barrier_init(&barrier, NULL, NO_PROCESS); 

    for (i = 0; i < NO_PROCESS; i++) 
     pthread_create (&threads[i], NULL, (void *) &work, (void *) i); 

    for (i = 0; i < NO_PROCESS; i++) 
     pthread_join(threads[i], NULL); 

    pthread_barrier_destroy(&barrier); 

    if (PRINT == 1) 
     Print_Matrix(); 
} 

void work(void *pId) 
{ 
    int i, j, k; 
    long thread_id = (long)pId; 

    /* Gaussian elimination algorithm */ 

    for (k = 0; k < N; k++) 
    { /* Outer loop */ 

     if (thread_id == (k % NO_PROCESS)) 
     { 
      for (j = k + 1;(j < N); j++) 
       A[k][j] = A[k][j]/A[k][k]; /* Division step */ 

      y[k] = b[k]/A[k][k]; 

      A[k][k] = 1.0; 
     } 

     pthread_barrier_wait(&barrier); /* wait for other threads finishing this round */ 

     for (i = k + 1;(i < N); i++) 
     { 
      if (thread_id == (i % NO_PROCESS)) 
      { 
       for (j = k + 1;(j < N); j++) 
        A[i][j] = A[i][j] - A[i][k] * A[k][j]; /* Elimination step */ 

       b[i] = b[i] - A[i][k] * y[k]; 

       A[i][k] = 0.0; 
      } 
     } 

     pthread_barrier_wait(&barrier); /* wait for other threads finishing this round */ 

    } 
} 

void Init_Matrix() 
{ 
    int i, j; 

    printf("\nsize  = %dx%d ", N, N); 
    printf("\nmaxnum = %d \n", maxnum); 
    printf("Init  = %s \n", Init); 
    printf("Initializing matrix..."); 

    if (strcmp(Init, "rand") == 0) 
    { 
     for (i = 0; i < N; i++) 
     { 
      for (j = 0; j < N; j++) 
      { 
       if (i == j) /* diagonal dominance */ 
        A[i][j] = (double)(rand() % maxnum) + 5.0; 
       else 
        A[i][j] = (double)(rand() % maxnum) + 1.0; 
      } 
     } 
    } 

    if (strcmp(Init, "fast") == 0) 
    { 
     for (i = 0; i < N; i++) 
     { 
      for (j = 0; j < N; j++) 
      { 
       if (i == j) /* diagonal dominance */ 
        A[i][j] = 5.0; 
       else 
        A[i][j] = 2.0; 
      } 
     } 
    } 

    /* Initialize vectors b and y */ 
    for (i = 0; i < N; i++) 
    { 
     b[i] = 2.0; 
     y[i] = 1.0; 
    } 

    printf("done \n\n"); 

    if (PRINT == 1) 
     Print_Matrix(); 
} 

void Print_Matrix() 
{ 
    int i, j; 

    printf("Matrix A:\n"); 

    for (i = 0; i < N; i++) 
    { 
     printf("["); 

     for (j = 0; j < N; j++) 
      printf(" %5.2f,", A[i][j]); 

     printf("]\n"); 
    } 

    printf("Vector b:\n["); 

    for (j = 0; j < N; j++) 
     printf(" %5.2f,", b[j]); 

    printf("]\n"); 

    printf("Vector y:\n["); 

    for (j = 0; j < N; j++) 
     printf(" %5.2f,", y[j]); 

    printf("]\n"); 

    printf("\n\n"); 
} 

void Init_Default() 
{ 
    N = 2048; 
    Init = "rand"; 
    maxnum = 15.0; 
    PRINT = 0; 
} 

int Read_Options(int argc, char **argv) 
{ 
    char *prog; 
    prog = *argv; 

    while (++argv, --argc > 0) 
     if (**argv == '-') 
      switch (*++*argv) 
      { 

        case 'n': 
        --argc; 
        N = atoi(*++argv); 
        break; 

        case 'h': 
        printf("\nHELP: try sor -u \n\n"); 
        exit(0); 
        break; 

        case 'u': 
        printf("\nUsage: sor [-n problemsize]\n"); 
        printf("   [-D] show default values \n"); 
        printf("   [-h] help \n"); 
        printf("   [-I init_type] fast/rand \n"); 
        printf("   [-m maxnum] max random no \n"); 
        printf("   [-P print_switch] 0/1 \n"); 
        exit(0); 
        break; 

        case 'D': 
        printf("\nDefault: n   = %d ", N); 
        printf("\n   Init  = rand"); 
        printf("\n   maxnum = 5 "); 
        printf("\n   P   = 0 \n\n"); 
        exit(0); 
        break; 

        case 'I': 
        --argc; 
        Init = *++argv; 
        break; 

        case 'm': 
        --argc; 
        maxnum = atoi(*++argv); 
        break; 

        case 'P': 
        --argc; 
        PRINT = atoi(*++argv); 
        break; 

        default: 
        printf("%s: ignored option: -%s\n", prog, *argv); 
        printf("HELP: try %s -u \n\n", prog); 
        break; 
      } 
} 

任何人都可以告诉我如何在Visual Studio中运行它。我知道应该包含一些标题,但我不知道该怎么做。

请让我从开始到结束一步一步告诉我。我在编程初学者请告诉我一步一步...

+0

你可以在你的机器上安装Linux。它有一个相当不错的posix线程实现。它会让你学到很多东西! – 2011-12-14 06:05:43

回答

3

http://sourceware.org/pthreads-win32/

上面的链接是在Win32一个并行线程部分实现。这有点旧,但它应该完成这项工作。

+0

谢谢,我已经找到了在visual studio中使用pthread的方式,使用你给我的链接。我已经完成了所有步骤,比如添加include文件夹和lib文件夹并添加.ddl文件。现在我有pthread header.but有一个问题。我可以成功地建立我的项目,但当我去调试它,我面临以下错误:“”程序不能启动,因为pthreadVC2.dll从您的computer.try重新安装程序来解决问题缺少“”。范你告诉我有关错误和如何解决? – user642564 2011-12-15 04:04:28

3

现在,你已经得到了你的程序编译,需要在两个地方之一,以找到特定的DLL(pthreadVC2.dll):

  • 1)您的.exe文件位于同一目录
  • 2)C:\ Windows \ System32下(如果你的操作系统是32位Windows版本)
  • 2)C:\ WINDOWS \ SysWOW64中(如果你的操作系统是64位Windows版本)

真的,“C:\ WINDOWS”在上面的例子应该是你的Windows安装到实际的路径,有时这也被拼写为%WINDIR%\ SYSTEM32或%WINDIR%\ Syswow64资料

0

短并且易于描述可以发现here (对于所有文件:* .h,* .lib,* .dll)。