任务是使用 p 线程并行化矩阵乘法并使用英特尔 ISPC 编译器进行矢量化

Task is to parallelize matrix multiplication with p-threads and vectorized with Intel ISPC compiler

在.ispc文件中使用pthread产生如下错误: (1) t.ispc:2:13: Error: Illegal to return a "varying" or vector 类型 来自导出函数 "matrix_mult_pl" 导出 void * matrix_mult_pl( void *arg )

(2) t.ispc:2:36: 错误:可变指针类型参数 "arg" 是 在导出函数中是非法的。 导出 void * matrix_mult_pl( void *arg )

(3) t.ispc:6:11: 错误:语法错误,意外 'int'。 tid = *(int *)(arg); // 获取顺序分配的线程 ID。 ^^^

还有更多错误。编码器附在下面。 请查看在 ISPC 中使用 pthreads 的问题。

threads.c 文件
/**
 * Thread routine.
 * Each thread works on a portion of the 'matrix1'.
 * The start and end of the portion depend on the 'arg' which
 * is the ID assigned to threads sequentially. 
 */
void * matrix_mult_pl( void *arg )
{
  int rows, cols, j, tid, portion_size, row_start, row_end;

  tid = *(int *)(arg); // get the thread ID assigned sequentially.
  portion_size = size / num_threads;
  row_start = tid * portion_size;
  row_end = (tid+1) * portion_size;

  for (rows = row_start; rows < row_end; ++rows) { // hold row index of 'matrix1'
    for (j = 0; j < size; ++j) { // hold column index of 'matrix2'
     // hold value of a cell
      /* one pass to sum the multiplications of corresponding cells
     in the row vector and column vector. */
      for(cols=0; cols<size; cols++) { 
        result_pl[ rows ][ cols ] += matrix1[ rows ][ j ] * matrix2[ j ][ cols ];
      }
    }
  }
}

threads.ispc 文件

export void * matrix_mult_pl( void *arg )
{
  int rows, cols, j, tid, portion_size, row_start, row_end;

  tid = *(int *)(arg); // get the thread ID assigned sequentially.
  portion_size = size / num_threads;
  row_start = tid * portion_size;
  row_end = (tid+1) * portion_size;

  for (rows = row_start; rows < row_end; ++rows) { // hold row index of 'matrix1'
    for (j = 0; j < size; ++j) { // hold column index of 'matrix2'
     // hold value of a cell
      /* one pass to sum the multiplications of corresponding cells
     in the row vector and column vector. */
      for(cols=0; cols<size; cols++) { 
        result_pl[ rows ][ cols ] += matrix1[ rows ][ j ] * matrix2[ j ][ cols ];
      }
    }
  }
}

为什么 ISPC 文件没有通过 pthreads 并行化矢量化执行?

您遇到的问题是因为 ISPC 默认为 varying 类型。 int x = 0varying int x = 0 相同。这同样适用于指针类型 void *argvoid varying * uniform arg 并且您不能在导出函数中使用不同的类型。在移植和首次开始使用 ISPC 时,最好使用 uniformvarying 关键字进行明确说明。