8 Replies - 1326 Views - Last Post: 10 December 2014 - 09:45 AM Rate Topic: -----

#1 user127888  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-December 14

CUDA C program to count odd numbers in an array using shared memory

Posted 09 December 2014 - 09:57 PM

I need help near checking the if condition for odd numbers. I'm getting an error. Could you please help me resolve it

#include<stdio.h>
#include<stdlib.h>
#include<cuda.h>
#include<cuda_runtime.h>

#define BLOCKSIZE 1

__global__ void odd1(int *total, int *odd, int N){
	int tx = threadIdx.x;
	__shared__ float s_data[BLOCKSIZE];
	
	int idx = blockDim.x * blockIdx.x + tx;
	s_data[tx] = total[idx];
	__syncthreads();
	int l = 0;	
	for(l=0;l<N;l++){
	//if(s_data[tx]%2 == 1) {
	if((tx & 0x0001) == 0x0000)	
		
			*odd++;
		
		
	}

}
int main(){
	int *totalhost, *oddhost, *totaldevice, *odddevice;
	const int N =10;
	
	cudaMalloc((void**)&totaldevice, N*sizeof(int));
	cudaMalloc((void**)&odddevice, N*sizeof(int));
	cudaMemset(odddevice, 0, N*sizeof(int));
	int k =0;
	for(k=0;k<N;k++){
		totalhost[k]=k+1;
	}

    cudaMemcpy(totaldevice, totalhost, N*sizeof(int),cudaMemcpyHostToDevice);
    
    odd1<<<1,BLOCKSIZE>>>(totaldevice, odddevice, N);
    
    cudaMemcpy(oddhost, odddevice, N*sizeof(int), cudaMemcpyDeviceToHost);
    
    printf("odd numbers; \n");
    int l = 0;
   // for(l=0;l<N;l++)
    	printf("%d\n",oddhost);
    
    cudaFree(totaldevice);
    cudaFree(odddevice);
    
    return 0;
    
}

This post has been edited by modi123_1: 10 December 2014 - 07:56 AM
Reason for edit:: Please use the 'code' button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: CUDA C program to count odd numbers in an array using shared memory

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,843
  • Joined: 12-June 08

Re: CUDA C program to count odd numbers in an array using shared memory

Posted 10 December 2014 - 07:56 AM

Please post the error text.
Was This Post Helpful? 0
  • +
  • -

#3 user127888  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-December 14

Re: CUDA C program to count odd numbers in an array using shared memory

Posted 10 December 2014 - 09:25 AM

odd.cu(17) error: expression must have integral or enum type

odd.cu(26): error: expected a ";"

odd.cu(47): warning: parsing restarts here after previous syntax error

odd.cu(49): error: identifier "totaldevice" is undefined

odd.cu(50): error: identifier "odddevice" is undefined

odd.cu(52): error: return value type does not match the function type

5 errors detected in the compilation of "/tmp/tmpxft_000017d2_00000000-6_odd.cpp1.ii".
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,843
  • Joined: 12-June 08

Re: CUDA C program to count odd numbers in an array using shared memory

Posted 10 December 2014 - 09:27 AM

I would first start by looking at lines 08-25. I see three open braces but only two ending braces.
Was This Post Helpful? 0
  • +
  • -

#5 user127888  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-December 14

Re: CUDA C program to count odd numbers in an array using shared memory

Posted 10 December 2014 - 09:35 AM

thanks, after putting the curly braces, the error is

odd.cu(17): error: expression must have integral or enum type

odd.cu(45): warning: variable "oddhost" is used before its value is set

I need help in resolving error 17

line 17 is if(s_data[tx]%2 == 1) {

I have commented that out, but I am using that for finding out the odd numbers
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,843
  • Joined: 12-June 08

Re: CUDA C program to count odd numbers in an array using shared memory

Posted 10 December 2014 - 09:35 AM

What is at line 17? Are you certain you are not mixing up the data types on a parameter or return value?
Was This Post Helpful? 0
  • +
  • -

#7 user127888  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-December 14

Re: CUDA C program to count odd numbers in an array using shared memory

Posted 10 December 2014 - 09:39 AM

line 17 is

if(s_data[tx]%2 == 1) {


s_data is a floating shared array, I am just using percentile. Can we use percentile in CUDA?
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: CUDA C program to count odd numbers in an array using shared memory

Posted 10 December 2014 - 09:44 AM

s_data is an array of float. You can't use the standard mod operator ( % ) on floats. There is fmod(), depending on what exactly you're trying to do. Or you can truncate the decimal before you use %. Again, it all depends on what you're trying to accomplish with that line.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,843
  • Joined: 12-June 08

Re: CUDA C program to count odd numbers in an array using shared memory

Posted 10 December 2014 - 09:45 AM

How about you search the docs on that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1