Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8498599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:26:50+00:00 2026-06-11T00:26:50+00:00

This is program for matrix multiplication on CUDA architecture. This code is working fine

  • 0

This is program for matrix multiplication on CUDA architecture.
This code is working fine when size of array is 30 x 30 but giving output as a series of 0’s when size is greater.
I am using standard ec2 instance for CUDA hosted on linux machine. Can anybody figure out the reason ?

#include <stdio.h>
#define SIZE 30

__global__ void matrix_multiply(float *input1,float  *input2,float *output,int dimension){


    int input1_index = threadIdx.x / dimension * dimension;
    int input2_index =  threadIdx.x % dimension;
    int i=0;
    for( i =0; i <dimension; i++){
        output[threadIdx.x] += input1[input1_index + i] * input2[input2_index + i * dimension];
    }
}
int main(){
    int i,j,natural_number=1;
    float input1[SIZE][SIZE],input2[SIZE][SIZE],result[SIZE][SIZE]={0};
    float  *c_input1,*c_input2,*c_result;
    for(i=0;i<SIZE;i++){
        for(j=0;j<SIZE;j++){
            input1[i][j]=input2[i][j]=natural_number++;
        }
    }
    cudaMalloc((void**)&c_input1,sizeof(input1));
    cudaMalloc((void**)&c_input2,sizeof(input2));
    cudaMalloc((void**)&c_result,sizeof(result));
    cudaMemcpy(c_input1,input1,sizeof(input1),cudaMemcpyHostToDevice);
    cudaMemcpy(c_input2,input2,sizeof(input2),cudaMemcpyHostToDevice);
    cudaMemcpy(c_result,result,sizeof(result),cudaMemcpyHostToDevice);

    matrix_multiply<<<1,SIZE * SIZE>>>(c_input1,c_input2,c_result,SIZE);
    if(cudaGetLastError()!=cudaSuccess){
        printf("%s\n",cudaGetErrorString(cudaGetLastError()));
    }
    cudaMemcpy(result,c_result,sizeof(result),cudaMemcpyDeviceToHost);
    for(i=0;i<SIZE;i++){
        for(j=0;j<SIZE;j++){
            printf("%.2f ",result[i][j]);
        }
        printf("\n");
    }
    cudaFree(c_input1);
    cudaFree(c_input2);
    cudaFree(c_result); 
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:26:51+00:00Added an answer on June 11, 2026 at 12:26 am

    You probably have a max of 1024 threads per block on your GPU. 30 x 30 = 900, so that should be OK, but e.g. 40 x 40 would results in a kernel launch failure (take-home message: always check for errors !).

    You probably want to consider organizing your data differently, e.g. SIZE blocks of SIZE threads and then call the kernel as:

    matrix_multiply<<<SIZE, SIZE>>>(c_input1,c_input2,c_result,SIZE);
    

    (Obviously you’ll need to modify your array indexing within the kernel code, e.g. use the block index as the row and the thread index as the column.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code for a matrix multiplication program ex implicit none real ::
This program runs just fine, but for some reason when I ask for an
I wrote this matrix addition program and I dont know why but I keep
I made this program that should flood fill the matrix but something went wrong.
I keep reading and reading this matrix multiplication kernel code and I just don't
This program is meant to generate a dynamic array, however it gives an access
I was running the MapReduce Matrix Multiplication program found at http://www.norstad.org/matrix-multiply/index.html . I found
I'm performing matrix multiplication with this simple algorithm. To be more flexible I used
I was developing a program to find matrix multiplication. #include <iostream> using namespace std;
I'm running a completely parallel matrix multiplication program on a Mac Pro with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.