نمایش نتایج 1 تا 5 از 5

نام تاپیک: الگوریتم ضرب ماتریسی به صورت موازی

  1. #1
    کاربر دائمی
    تاریخ عضویت
    مهر 1387
    محل زندگی
    بابل
    پست
    112

    الگوریتم ضرب ماتریسی به صورت موازی

    سلام دوستان عزیز. استاد من، یکسری از الگوریتمهای ضرب موازی ماتریس ها را برای ما توضیح داده، مثل تقسیم ماتریس به 8 پردازنده ثابت، n*n مستقیم(n* پردازنده)، بلوکی s*s(s تعداد پردازنده)،Recursive، cannon n*n، کانون بلوکی، fox n*n و فاکس بلوکی.
    حالا سوال، ازمون خواسته یکی دیگه از الگوریتمهای ضرب موازی را تحلیل کنیم. کسی میتونه الگوریتمی معرفی کنه برای این مسئله. با تعداد n پردازنده

  2. #2

    نقل قول: الگوریتم ضرب ماتریسی به صورت موازی

    به این و این یکی سر بزن!کتابی به نام معماری و طراحی سیستم های پیشرفته کامپیوتری و پردازش موازی چاپ دانشگاه علم و صنعت ایران سال 1378(البته مطمئن نیستم که که مطلبی در این مورد داشته باشه چون محتوای کاملش الان دقیق یادم نمیاد)؛ و گوگل!

  3. #3
    کاربر دائمی آواتار مصطفی ساتکی
    تاریخ عضویت
    اردیبهشت 1386
    محل زندگی
    www.7khatcode.com
    پست
    1,193

    نقل قول: الگوریتم ضرب ماتریسی به صورت موازی

    به طور مثال این کد تو زبان cuda دو تا ماتریس رو به صورت موازی ضرب می کنه.

    // Matrices are stored in row-major order:
    // M(row, col) = *(M.elements + row * M.stride + col)
    typedef struct {
    int width;
    int height;
    int stride;
    float* elements;
    } Matrix;
    // Get a matrix element
    __device__ float GetElement(const Matrix A, int row, int col)
    {
    return A.elements[row * A.stride + col];
    }
    // Set a matrix element
    __device__ void SetElement(Matrix A, int row, int col,
    float value)
    {
    A.elements[row * A.stride + col] = value;
    }
    // Get the BLOCK_SIZExBLOCK_SIZE sub-matrix Asub of A that is
    // located col sub-matrices to the right and row sub-matrices down
    // from the upper-left corner of A
    __device__ Matrix GetSubMatrix(Matrix A, int row, int col)
    {
    Matrix Asub;
    Asub.width = BLOCK_SIZE;
    Asub.height = BLOCK_SIZE;
    Asub.stride = A.stride;
    Asub.elements = &A.elements[A.stride * BLOCK_SIZE * row
    + BLOCK_SIZE * col];
    return Asub;
    }
    // Thread block size
    #define BLOCK_SIZE 16
    // Forward declaration of the matrix multiplication kernel
    __global__ void MatMulKernel(const Matrix, const Matrix, Matrix);
    // Matrix multiplication - Host code
    // Matrix dimensions are assumed to be multiples of BLOCK_SIZE
    void MatMul(const Matrix A, const Matrix N, Matrix C)
    {
    // Load A and
    d_A.width = d_A.stride = A.width; d_A.height = A.height;
    size_t size = A.width * A.height * sizeof(float);
    cudaMalloc((void**)&d_A.elements, size);
    cudaMemcpy(d_A.elements, A.elements, size,
    cudaMemcpyHostToDevice);
    Matrix d_B;
    d_B.width = d_B.stride = B.width; d_B.height = B.height;
    size = B.width * B.height * sizeof(float);
    cudaMalloc((void**)&d_B.elements, size);
    cudaMemcpy(d_B.elements, B.elements, size,
    cudaMemcpyHostToDevice);
    // Allocate C in device memory
    Matrix d_C;
    d_C.width = d_C.stride = C.width; d_C.height = C.height;
    size = C.width * C.height * sizeof(float);
    cudaMalloc((void**)&d_C.elements, size);
    // Invoke kernel
    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
    dim3 dimGrid(B.width / dimBlock.x, A.height / dimBlock.y);
    MatMulKernel<<<dimGrid, dimBlock>>>(d_A, d_B, d_C);
    // Read C from device memory
    cudaMemcpy(C.elements, d_C.elements, size,
    cudaMemcpyDeviceToHost);
    // Free device memory
    cudaFree(d_A.elements);
    cudaFree(d_B.elements);
    cudaFree(d_C.elements);
    }
    // Matrix multiplication kernel called by MatrixMul()
    __global__ void MatMulKernel(Matrix A, Matrix B, Matrix C)
    {
    // Block row and column
    int blockRow = blockIdx.y;
    int blockCol = blockIdx.x;
    // Each thread block computes one sub-matrix Csub of C
    Matrix Csub = GetSubMatrix(C, blockRow, blockCol);
    // Each thread computes one element of Csub
    // by accumulating results into Cvalue
    float Cvalue = 0;
    // Thread row and column within Csub
    int row = threadIdx.y;
    int col = threadIdx.x;
    // Loop over all the sub-matrices of A and B that are
    // required to compute Csub
    // Multiply each pair of sub-matrices together
    // and accumulate the results
    for (int m = 0; m < (A.width / BLOCK_SIZE); ++m) {// Get sub-matrix Asub of A
    Matrix Asub = GetSubMatrix(A, blockRow, m);
    // Get sub-matrix Bsub of B
    Matrix Bsub = GetSubMatrix(B, m, blockCol);
    // Shared memory used to store Asub and Bsub respectively
    __shared__ float As[BLOCK_SIZE][BLOCK_SIZE];
    __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
    // Load Asub and Bsub from device memory to shared memory
    // Each thread loads one element of each sub-matrix
    As[row][col] = GetElement(Asub, row, col);
    Bs[row][col] = GetElement(Bsub, row, col);
    // Synchronize to make sure the sub-matrices are loaded
    // before starting the computation
    __syncthreads();
    // Multiply Asub and Bsub together
    for (int e = 0; e < BLOCK_SIZE; ++e)
    Cvalue += As[row][e] * Bs[e][col];
    // Synchronize to make sure that the preceding
    // computation is done before loading two new
    // sub-matrices of A and B in the next iteration
    __syncthreads();
    }
    // Write Csub to device memory
    // Each thread writes one element
    SetElement(Csub, row, col, Cvalue);
    }

  4. #4
    کاربر دائمی
    تاریخ عضویت
    مهر 1387
    محل زندگی
    بابل
    پست
    112

    نقل قول: الگوریتم ضرب ماتریسی به صورت موازی

    نقل قول نوشته شده توسط mostafa.sataki مشاهده تاپیک
    به طور مثال این کد تو زبان cuda دو تا ماتریس رو به صورت موازی ضرب می کنه.

    // Matrices are stored in row-major order:
    // M(row, col) = *(M.elements + row * M.stride + col)
    typedef struct {
    int width;
    int height;
    int stride;
    float* elements;
    } Matrix;
    // Get a matrix element
    __device__ float GetElement(const Matrix A, int row, int col)
    {
    return A.elements[row * A.stride + col];
    }
    // Set a matrix element
    __device__ void SetElement(Matrix A, int row, int col,
    float value)
    {
    A.elements[row * A.stride + col] = value;
    }
    // Get the BLOCK_SIZExBLOCK_SIZE sub-matrix Asub of A that is
    // located col sub-matrices to the right and row sub-matrices down
    // from the upper-left corner of A
    __device__ Matrix GetSubMatrix(Matrix A, int row, int col)
    {
    Matrix Asub;
    Asub.width = BLOCK_SIZE;
    Asub.height = BLOCK_SIZE;
    Asub.stride = A.stride;
    Asub.elements = &A.elements[A.stride * BLOCK_SIZE * row
    + BLOCK_SIZE * col];
    return Asub;
    }
    // Thread block size
    #define BLOCK_SIZE 16
    // Forward declaration of the matrix multiplication kernel
    __global__ void MatMulKernel(const Matrix, const Matrix, Matrix);
    // Matrix multiplication - Host code
    // Matrix dimensions are assumed to be multiples of BLOCK_SIZE
    void MatMul(const Matrix A, const Matrix N, Matrix C)
    {
    // Load A and
    d_A.width = d_A.stride = A.width; d_A.height = A.height;
    size_t size = A.width * A.height * sizeof(float);
    cudaMalloc((void**)&d_A.elements, size);
    cudaMemcpy(d_A.elements, A.elements, size,
    cudaMemcpyHostToDevice);
    Matrix d_B;
    d_B.width = d_B.stride = B.width; d_B.height = B.height;
    size = B.width * B.height * sizeof(float);
    cudaMalloc((void**)&d_B.elements, size);
    cudaMemcpy(d_B.elements, B.elements, size,
    cudaMemcpyHostToDevice);
    // Allocate C in device memory
    Matrix d_C;
    d_C.width = d_C.stride = C.width; d_C.height = C.height;
    size = C.width * C.height * sizeof(float);
    cudaMalloc((void**)&d_C.elements, size);
    // Invoke kernel
    dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);
    dim3 dimGrid(B.width / dimBlock.x, A.height / dimBlock.y);
    MatMulKernel<<<dimGrid, dimBlock>>>(d_A, d_B, d_C);
    // Read C from device memory
    cudaMemcpy(C.elements, d_C.elements, size,
    cudaMemcpyDeviceToHost);
    // Free device memory
    cudaFree(d_A.elements);
    cudaFree(d_B.elements);
    cudaFree(d_C.elements);
    }
    // Matrix multiplication kernel called by MatrixMul()
    __global__ void MatMulKernel(Matrix A, Matrix B, Matrix C)
    {
    // Block row and column
    int blockRow = blockIdx.y;
    int blockCol = blockIdx.x;
    // Each thread block computes one sub-matrix Csub of C
    Matrix Csub = GetSubMatrix(C, blockRow, blockCol);
    // Each thread computes one element of Csub
    // by accumulating results into Cvalue
    float Cvalue = 0;
    // Thread row and column within Csub
    int row = threadIdx.y;
    int col = threadIdx.x;
    // Loop over all the sub-matrices of A and B that are
    // required to compute Csub
    // Multiply each pair of sub-matrices together
    // and accumulate the results
    for (int m = 0; m < (A.width / BLOCK_SIZE); ++m) {// Get sub-matrix Asub of A
    Matrix Asub = GetSubMatrix(A, blockRow, m);
    // Get sub-matrix Bsub of B
    Matrix Bsub = GetSubMatrix(B, m, blockCol);
    // Shared memory used to store Asub and Bsub respectively
    __shared__ float As[BLOCK_SIZE][BLOCK_SIZE];
    __shared__ float Bs[BLOCK_SIZE][BLOCK_SIZE];
    // Load Asub and Bsub from device memory to shared memory
    // Each thread loads one element of each sub-matrix
    As[row][col] = GetElement(Asub, row, col);
    Bs[row][col] = GetElement(Bsub, row, col);
    // Synchronize to make sure the sub-matrices are loaded
    // before starting the computation
    __syncthreads();
    // Multiply Asub and Bsub together
    for (int e = 0; e < BLOCK_SIZE; ++e)
    Cvalue += As[row][e] * Bs[e][col];
    // Synchronize to make sure that the preceding
    // computation is done before loading two new
    // sub-matrices of A and B in the next iteration
    __syncthreads();
    }
    // Write Csub to device memory
    // Each thread writes one element
    SetElement(Csub, row, col, Cvalue);
    }
    بابتشون ممنون. ولی من کوبول بلدد نیستم. ولی باز هم ممنون

  5. #5

    نقل قول: الگوریتم ضرب ماتریسی به صورت موازی

    سلام ممنون از اطلاعات مفیدتون
    ولی main نداره چرا؟
    ممنون

تاپیک های مشابه

  1. برج هانوی به صورت موازی
    نوشته شده توسط maryamghani در بخش الگوریتم، کامپایلر، هوش مصنوعی و ساختمان داده ها
    پاسخ: 21
    آخرین پست: یک شنبه 14 آذر 1389, 09:52 صبح
  2. برنامه یا الگوریتم ضرب استراسن
    نوشته شده توسط nobody777 در بخش الگوریتم، کامپایلر، هوش مصنوعی و ساختمان داده ها
    پاسخ: 15
    آخرین پست: جمعه 28 آبان 1389, 07:39 صبح
  3. سوال: الگوریتم ضرب استذاسن به صورت بازگشتی
    نوشته شده توسط sahar.1986 در بخش الگوریتم، کامپایلر، هوش مصنوعی و ساختمان داده ها
    پاسخ: 1
    آخرین پست: جمعه 21 فروردین 1388, 02:07 صبح
  4. اجرای برنامه به صورت موازی
    نوشته شده توسط hbi در بخش برنامه نویسی مبتنی بر Microsoft .Net Framework
    پاسخ: 0
    آخرین پست: دوشنبه 29 بهمن 1386, 18:50 عصر

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •