资源简介
基于多线程的实现矩阵相乘的openmp程序,采用cannon算法,采用共享任务结构的sections(功能并行性)。
代码片段和文件信息
#include “omp.h“
#include “conio.h“
#include “stdio.h“
#include “stdlib.h“
#include “math.h“
#define N 160
void MoveDataBlockLine_A( int A[][N] int lineNumber ) // A 左移一块
{
int ij k jump=N/4;
int tmp[N/4][N/4];
for( i=0; i for( j=0; j tmp[i][j] = A[i+jump*lineNumber][j]; // 保存块 A(i0)
for( k=0; k<3; k++ )
{
for( i=0; i for( j=0; j A[i+jump*lineNumber][j+jump*k] = A[i+jump*lineNumber][j+jump*(k+1)];
}
for( i=0; i for( j=0; j A[i+jump*lineNumber][j+jump*3] = tmp[i][j];
}
void MoveDataBlockLine_B( int B[][N] int lineNumber ) // B 上移一块
{
int ij k jump=N/4;
int tmp[N/4][N/4];
for( i=0; i for( j=0; j tmp[i][j] = B[i][j+jump*lineNumber]; // 保存块 b(0j)
for( k=0; k<3; k++ )
{
for( i=0; i for( j=0; j B[i+jump*k][j+jump*lineNumber] = B[i+jump*(k+1)][j+jump*lineNumber];
}
for( i=0; i for( j=0; j B[i+jump*3][j+jump*lineNumber] = tmp[i][j];
}
void MutiplyBlock( int A[][N] int B[][N] int C[][N] int bi int bj )
{
int ijjump=N/4;
for( i=0; i for( j=0; j C[i+jump*bi][j+jump*bj] += A[i+jump*bi][j+jump*bj] * B[i+jump*bi][j+jump*bj];
}
int main()
{
FILE *fpA *fpB *fpC;
int i j k jump=N/4;
int A[N][N] B[N][N] C[N][N];
printf( “\n开始 Cannon 计算 ...“ );
fpA = fopen( “A.txt“ “w+“ );
fpB = fopen( “B.txt“ “w+“ );
/* 赋初值 */
for( i=0; i for( j=0; j {
A[i][j] = rand()%100; fprintf( fpA “\n A[%d][%d] = %d “ i j A[i][j] );
B[i][j] = rand()%100; fprintf( fpB “\n B[%d][%d] = %d “ i j B[i][j] );
C[i][j] = 0;
}
fclose(fpA);
fclose(fpB);
// 设置 omp 线程数
omp_set_num_threads( 2 );
if( N % 4 == 0 )
{
#pragma omp parallel shared( A B C ) private( ij )
{
#pragma omp sections
{
// 第一次移动数据
#pragma om
评论
共有 条评论