# Parrallelize radix type counter... variable base counter

Say you have a specific base you want your number to represented in, my case is 5. I want to produce all numbers increment in the specific base rather than base 10. What I am really trying to do is produce all possible vectors of a certain dimension with a finite set of coefficients. Reason is to take the fft of each one basically all vectors in this function space. I have been looking through libraries and what not but haven’t found a way so I was wondering if anyone had advice on how to speed this up:

``````    int bucket = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

while (bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5
&& bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5 && bucket != 5 ){
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {
bucket = 0;
bucket++;
if (bucket == 5) {

}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
printf("%i %i %i %i %i %i %i %i %i %i %i %i %i %i %i\n", bucket,bucket,bucket,bucket,bucket,bucket,bucket,bucket,bucket,bucket,bucket,bucket,bucket,bucket,bucket);
}
``````

there is obvious symmetry to it, it would be nice to do this on the device end rather than host so I dont have to copy over any… advice is appreciated!

using cuda 10 on jetson agx

but idk increments depends on the previous like clock increment does that affect its ability to be run in a grid?

1 Like
``````
#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* strtoul */
#include <iostream>
#include <math.h>

#define a 300000000

int main()
{
double vec = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
//    for (int i = 0; i < a; i++) {
//    printf("%i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i\n",
//    vec = 0.000000000032768;
vec = 0.00000000016384;
vec = 0.0000000008192;
vec = 0.000000004;
vec = 0.00000002;
vec = 0.000000102;
vec = 0.000000512;
vec = 0.00000256;
vec = 0.0000128;
vec = 0.000064;
vec = 0.00032;
vec = 0.0016;
vec = 0.008;
vec = 0.04;
vec = 0.2;
vec = 1;

for (int i = 0; i < 300000000; i++) {
printf("%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n",
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)),
floor(fmod(i*vec, 5)));
}

}
``````

update all math functions are in CUDA math sdk planning on using shared memory for the vec constants… will need to make the big conditional in the for 5^15 somehow

for loop will have to be changed to something like while(tid < N)

kinda cool how dividing 1 by 5^n is 2^n left shift n wouldnt have guessed lol

oh damn maybe dont need those constants

``````// includes, system
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// includes, project
#include <cuda_runtime.h>
#include "common/inc/helper_cuda.h"
#include "common/inc/helper_functions.h"
#include <cuda.h>

//const int blocks_per_grid = 256;
//const int N = 20480;

__global__ void radix_count(float * results, int N) {

//__shared__ float eyes[blocks_per_grid];

int tid = threadIdx.x + blockIdx.x * blockDim.x;

if (tid < N) {

}
}

int main() {

float h_input = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

float res_output;
float * d_input;
size_t pitch;
checkCudaErrors(cudaMallocPitch(reinterpret_cast<void **>(&d_input), &pitch, 15*sizeof(float), 16));
checkCudaErrors(cudaMemcpy(d_input, h_input, 15*16*sizeof(float), cudaMemcpyHostToDevice));

checkCudaErrors(cudaMemcpy(res_output, d_input, 15*16*sizeof(float), cudaMemcpyDeviceToHost));

for (int i = 0; i < 15; i++) {
for (int j = 0; j < 16; j++) {
printf("%f,", res_output[i][j]);
}
printf("\n");
}
return 0;
}
``````

not working yet but compiles and runs lol also dont think thats the right way to use shared memory

``````// includes, system
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// includes, project
#include <cuda_runtime.h>
#include "common/inc/helper_cuda.h"
#include "common/inc/helper_functions.h"
#include <cuda.h>

const int N = 1024;

__global__ void radix_count(float * results, float * radii, float * init_shared_mem, int n) {

__shared__ float eyes;
if (tid2 < 15) {
}

while (tid < n*15) {
tid += 15;
}
}

int main() {

float h_input[N*15];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 15; j++) {
h_input[j+(i*15)] = 0;
}
}
float h_constants = {1, 0.2, 0.008, 0.0016, 0.00032, 0.000064, 0.0000128, 0.00000256, 0.000000512, 0.000000102,
0.00000002, 0.000000004, 0.0000000008192, 0.00000000016384};
float res_output[N*15];
float * d_input;
float h_shared_mem = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float * d_shared_mem;
float * d_constants;

checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_input), 15*N*sizeof(float)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_constants), 15*sizeof(float)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_shared_mem), 15*sizeof(float)));
checkCudaErrors(cudaMemcpy(d_input, h_input, 15*N*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_constants, h_constants, 15*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_shared_mem, h_shared_mem, 15*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(res_output, d_input, 15*N*sizeof(float), cudaMemcpyDeviceToHost));

for (int i = 0; i < N; i++) {
for (int j = 0; j < 15; j++) {
printf("%f|", res_output[j+(i*15)]);
}
printf("\n");
}
return 0;
}

lets goo!!!!! no idea whether its more efficient than cpu rn lol
``````
``````// includes, system
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// includes, project
#include <cuda_runtime.h>
#include "common/inc/helper_cuda.h"
#include "common/inc/helper_functions.h"
#include <cuda.h>

const int N = 16;

__global__ void radix_count(float * results, float * radii, float * init_shared_mem, float * init_map, int n) {

__shared__ float eyes;
__shared__ float map;
for (int i = 0; i < 5; i++) {
map[i] = init_map[i];
}
if (tid2 < 15) {
}

if (tid < n*15) {
//tid += 15;
}
}

int main() {
float h_map = {7.6,8.7,9.8,10.9,11.10};
int h_input[N*15];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 15; j++) {
h_input[j+(i*15)] = 0;
}
}
float h_constants = {1, 0.2, 0.008, 0.0016, 0.00032, 0.000064, 0.0000128, 0.00000256, 0.000000512, 0.000000102,
0.00000002, 0.000000004, 0.0000000008192, 0.00000000016384};
float res_output[N*15];
float * d_input;
float h_shared_mem = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float * d_shared_mem;
float * d_constants;
float * d_map;
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_map), 5*sizeof(float)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_input), 15*N*sizeof(float)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_constants), 15*sizeof(float)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_shared_mem), 15*sizeof(float)));
checkCudaErrors(cudaMemcpy(d_map, h_map, 5*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_input, h_input, 15*N*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_constants, h_constants, 15*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_shared_mem, h_shared_mem, 15*sizeof(float), cudaMemcpyHostToDevice));
radix_count<<<N,15, 15*sizeof(float)+5*sizeof(float)>>>(d_input, d_constants, d_shared_mem, d_map, N);
checkCudaErrors(cudaMemcpy(res_output, d_input, 15*N*sizeof(float), cudaMemcpyDeviceToHost));

for (int i = 0; i < N; i++) {
for (int j = 0; j < 15; j++) {
printf("%f|", res_output[j+(i*15)]);
}
printf("\n");
}
return 0;
}
``````
``````// includes, system
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// includes, project
#include <cuda_runtime.h>
#include "common/inc/helper_cuda.h"
#include "common/inc/helper_functions.h"
#include <cuda.h>

const int N = 16;

__global__ void radix_count(float2 * results, float * radii, float * init_shared_mem, float2 * init_map, int n) {

__shared__ float eyes;
__shared__ float2 map;
for (int i = 0; i < 5; i++) {
map[i] = init_map[i];
}
if (tid2 < 15) {
}

if (tid < n*15) {
//tid += 15;
}
}

int main() {
float2 h_input[N*15];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 15; j++) {
h_input[j+(i*15)].x = 0;
h_input[j+(i*15)].y = 0;
}
}
float h_constants = {1, 0.2, 0.008, 0.0016, 0.00032, 0.000064, 0.0000128, 0.00000256, 0.000000512, 0.000000102,
0.00000002, 0.000000004, 0.0000000008192, 0.00000000016384};
float2 h_map;
h_map.x = 0;
h_map.y = 0;
h_map.x = 1;
h_map.y = 0;
h_map.x = -1;
h_map.y = 0;
h_map.x = 0;
h_map.y = 1;
h_map.x = 0;
h_map.y = -1;
float2 * d_map;
float2 res_output[N*15];
float2 * d_input;
float h_shared_mem = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float * d_shared_mem;
float * d_constants;

checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_map), 5*sizeof(float2)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_input), 15*N*sizeof(float2)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_constants), 15*sizeof(float)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_shared_mem), 15*sizeof(float)));
checkCudaErrors(cudaMemcpy(d_map, h_map, 5*sizeof(float2), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_input, h_input, 15*N*sizeof(float2), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_constants, h_constants, 15*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_shared_mem, h_shared_mem, 15*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(res_output, d_input, 15*N*sizeof(float2), cudaMemcpyDeviceToHost));

for (int i = 0; i < N; i++) {
for (int j = 0; j < 15; j++) {
printf("|%f,%f|", res_output[j+(i*15)].x, res_output[j+(i*15)].y);
}
printf("_\n");
}
checkCudaErrors(cudaFree(d_input));
checkCudaErrors(cudaFree(d_constants));
checkCudaErrors(cudaFree(d_shared_mem));
checkCudaErrors(cudaFree(d_map));
return 0;
}
``````
``````// includes, system
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// includes, project
#include <cuda_runtime.h>
#include "common/inc/helper_cuda.h"
#include "common/inc/helper_functions.h"
#include <cuda.h>

const int N = 16;

__global__ void radix_count(float2 * results, float * radii, float * init_shared_mem, float2 * init_map, int n) {

__shared__ float eyes;
__shared__ float2 map;
for (int i = 0; i < 5; i++) {
map[i] = init_map[i];
}
if (tid2 < 15) {
}

while (tid < n*15) {
tid += 15;
}
}

int main() {

float2 h_input[N*15];
for (int i = 0; i < N; i++) {
for (int j = 0; j < 15; j++) {
h_input[j+(i*15)].x = 0;
h_input[j+(i*15)].y = 0;
}
}
float h_constants = {1, 0.2, 0.008, 0.0016, 0.00032, 0.000064, 0.0000128, 0.00000256, 0.000000512, 0.000000102,
0.00000002, 0.000000004, 0.0000000008192, 0.00000000016384};
float2 h_map;
h_map.x = 0;
h_map.y = 0;
h_map.x = 1;
h_map.y = 0;
h_map.x = -1;
h_map.y = 0;
h_map.x = 0;
h_map.y = 1;
h_map.x = 0;
h_map.y = -1;
float2 * d_map;
float2 res_output[N*15];
float2 * d_input;
float h_shared_mem = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float * d_shared_mem;
float * d_constants;

checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_map), 5*sizeof(float2)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_input), 15*N*sizeof(float2)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_constants), 15*sizeof(float)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_shared_mem), 15*sizeof(float)));
checkCudaErrors(cudaMemcpy(d_map, h_map, 5*sizeof(float2), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_input, h_input, 15*N*sizeof(float2), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_constants, h_constants, 15*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(d_shared_mem, h_shared_mem, 15*sizeof(float), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpy(res_output, d_input, 15*N*sizeof(float2), cudaMemcpyDeviceToHost));

for (int i = 0; i < N; i++) {
for (int j = 0; j < 15; j++) {
printf("|%f,%f|", res_output[j+(i*15)].x, res_output[j+(i*15)].y);
}
printf("_\n");
}
checkCudaErrors(cudaFree(d_input));
checkCudaErrors(cudaFree(d_constants));
checkCudaErrors(cudaFree(d_shared_mem));
checkCudaErrors(cudaFree(d_map));
return 0;

}

``````
``````// includes, system
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// includes, project
#include <cuda_runtime.h>
#include "common/inc/helper_cuda.h"
#include "common/inc/helper_functions.h"
#include <cuda.h>

const int N = 1024;
const int M = 32;
const int vector_size = 15;

__global__ void kernel(float2 * result, float * init_eyes, float * init_coeff) {

__shared__ int eyes[N];
__shared__ float reduction_coeff[N];
if (tid < N) {
}
if (tid2 < N) {
}

int index_entry = blockIdx.x * blockDim.x + threadIdx.x;

if (index_entry < N*M) {
result[index_entry].x = floor(fmod(eyes[index_entry%N]*reduction_coeff[index_entry%N], (float)5));
}
}

void init_eyes(float * arr, int a, int b){
int cnt = 0;
int fill_val = 0;
int c = (int)a/b;
for (int i = 0; i < a; i++) {
if (cnt == b) {
fill_val++;
cnt = 0;
if (fill_val == c) {
fill_val = 0;
}
}
arr[i] = fill_val;
cnt++;
}
}

void init_reduction_coeff(float * arr, float * consts, int a, int b){
int c = (int)a/b;
int d = a%b;
for (int i = 0; i < a; i++) {
if (i >= (b-d-1)) {
arr[i] = 0;
}
arr[i] = consts[i%15];
}
}

int main() {
float h_constants = {1, 0.2, 0.04, 0.008, 0.0016, 0.00032, 0.000064, 0.0000128, 0.00000256, 0.000000512, 0.000000102,
0.00000002, 0.000000004, 0.0000000008192, 0.00000000016384};

// to hold result from gpu computation
float2 * d_input;
float2 res_output[N*M];
// vector to store initial shared mem
float h_eyes[N];
float * d_eyes;
float * d_constants;
// init host arrays to be transferred to shared mem
init_eyes(h_eyes, N, vector_size);

checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_input), M*N*sizeof(float2)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_eyes), N*sizeof(int)));
checkCudaErrors(cudaMalloc(reinterpret_cast<void **>(&d_constants), N*sizeof(float)));
checkCudaErrors(cudaMemcpy(d_eyes, h_eyes, N*sizeof(float), cudaMemcpyHostToDevice));
dim3 blocks_per_grid(M,1);

checkCudaErrors(cudaMemcpy(res_output, d_input, M*N*sizeof(float2), cudaMemcpyDeviceToHost));
for (int i = 0; i < N; i++) {
printf("%f,", res_output[i].x);
if ((i+1)%15 == 0) {
printf("\n");
}
}

checkCudaErrors(cudaFree(d_input));
checkCudaErrors(cudaFree(d_eyes));
}

``````

bit by bit boys ;) pun intended!!!

I’m not sure what is going on with this thread. Posting code or code updates with no written context at all isn’t particularly useful, and posting pictures with no context at all isn’t what this forum is about. Please desist from this activity. If you’re using this forum thread as your own personal scratchpad, please stop.

It didn’t seem like a big deal to track my progress on here, I was having fun learning and think CUDA is really cool. I apologize Robert I will desist from this activity.

There are great resources like github and pastebin for that. Thanks.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.