Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
hi!
my program can not return true final value from matrix c!!!
#include <stdio.h>
#include <conio.h>
 __global__ void kernelFunc(float* ad, float* bd, float* cd, int n) {
    int x = (blockIdx.x * blockDim.x) + threadIdx.x;
    int y = (blockIdx.y * blockDim.y) + threadIdx.y;
    
    if(x < n && y < n) {   
        float v = 0.0f;
        for(int i=0; i<n;>            v += ad[y * n + i] * bd[i * n + x];
        cd[y * n + x] = v;
    }
}
 
void main() {
 
int ii,jj,k;
    float* ad;
    float* bd;
    float* cd;
    int n=64;
	float a[64][64];
	float b[64][64];
	float c[64][64];
 
	for(ii=0;ii<n;ii++)>
	{
	for(jj=0;jj<n;jj++)>
	{
	a[ii][jj]=2;//ii+jj;
	b[ii][jj]=3;//ii+jj;
	//c[ii][jj]=4;
	}
 
	}
	
    cudaMalloc((void**)&ad, n * n * sizeof(float));
    cudaMalloc((void**)&bd, n * n * sizeof(float));
    cudaMalloc((void**)&cd, n * n * sizeof(float));
    
    cudaMemcpy(ad, a, n * n * sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(bd, b, n * n * sizeof(float), cudaMemcpyHostToDevice);
	cudaMemcpy(cd, c, n * n * sizeof(float), cudaMemcpyHostToDevice);
    dim3 block(1, 1);           
    dim3 grid(1, 1);
    
    kernelFunc<<<grid,>>>(ad, bd, cd, n);
 
    cudaMemcpy(c, cd, n * n * sizeof(float), cudaMemcpyDeviceToHost);
 
	for(ii=0;ii<n;ii++)>
	{
	printf("\n ");
	for(jj=0;jj<n;jj++)>
	{
	printf("%f ",c[ii][jj]);
 
	
 
	}
 
	}
    
    cudaFree(ad);
    cudaFree(bd);
    cudaFree(cd);
 
}
Posted 6-Aug-12 1:22am
Edited 6-Aug-12 1:26am
(no name)253.2K
v2
Comments
nv3 at 6-Aug-12 8:18am
   
So, what does your program return instead?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 320
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 184
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 6 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100