Click here to Skip to main content
15,564,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one, I am new with cuda
I have two arrays:

double* A = new double[]{1,2,3,4,5};

double* B = new double[]{2,2,2,3,3,3,4,4,4};


I want to find the index of the value of each element in A that is equal to each element in B, which in this case is:

int* index = {1,1,1,2,2,2,3,3,3};

but I get this

<pre>int* index = {0,0,0,0,0,0,0,0,0};


here is my code:
<pre>
#include "cuda_runtime.h"
#include "device_functions.h" 
#include "device_launch_parameters.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <cuda.h>

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "kernel.h"


using namespace std;

__global__ void add(int n, float* x, float* y)
{

	int index  = blockIdx.x * blockDim.x + threadIdx.x;
	int stride = blockDim.x * gridDim.x;
	for (int i = index; i < n; i+=stride)
	{
		y[i] = x[i] + y[i];
	}
}

__global__ void findIndex(int n, double* x, double* y, double* v)
{
	int index = threadIdx.x;

	for (int i = 0; i < n; i++)
	{
		if (y[i] = x[index])
		{
			v[index] = i;
		}
	}
}





int main() {

	double* A = new double[5]{1,2,3,4,5};
	double* B = new double[9] {2,2,2,3,3,3,4,4,4};
	double* C = new double[9];


	double* AA;
	double* BB;
	double* CC;


	cudaMallocManaged(&AA, 5 * sizeof(double));
	cudaMallocManaged(&BB, 9 * sizeof(double));
	cudaMallocManaged(&CC, 9 * sizeof(double));

	dim3 grid(9);
	findIndex << <grid, 1 >> > (9, BB,AA, CC);


	cudaDeviceSynchronize();
	for (int i = 0; i < 9; i++)
	{
		printf("{%d}", CC[i]);

	}

	int m = 0;


	return 0;


}



What I have tried:

I have tried everything but I do not get the results I want
Posted
Updated 14-Jun-21 1:10am

1 solution

I am not sure about the code I would write:
C++
findIndex(9, BB,AA, CC);

else you must store the result and set some useful return value.

You must compare each scalar of the array with the searched element value.

I recommand that you work with structs to be clearer in your code.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900