java - Enumerate all vectors of length N, in which each element can have a value of [0 ... K] and for which the sum of all elements is SUM -


i want enumerate vectors of length n, in each element can have value of [0 ... k] , sum of elements sum.

i solved problem using recursive function when retyped in cuda c got message cuda c doesn't support recursive functions. after made changes , rewrote function without using recursion, function boolean , not supported in cuda c because main global function must void without calling other functions. out of ideas, help?

the recursive function following:

    private static void computevectors(int[] n, int sum, int k, int k1, int i) {           if (sum == 0) {              printvectors(n, n.length);         } else if (i < n.length) {              (int j = k; j >= 0; j--) {                  if (j <= k1) {                     n[i] = j;                     computevectors(n, sum - j, sum - j, k1, + 1);                 }             }         }     }      private static void printvectors(int p[], int n) {          (int = 0; < n; i++) {             system.out.print(p[i] + " ");         }         system.out.println();     }      public static void main(string[] args) {          // todo code application logic here         computevectors(new int[4], 5, 3, 3, 0);      } 

the output example is:

3200 3110 3101 3020 3011 3002 2300 2210 2201 2120 2111 2102 2030 2021 2012 2003 1310 1301 1220 1211 1202 1130 1121 1112 1103 1031 1022 1013 0320 0311 0302 0230 0221 0212 0203 0131 0122 0113 0032

0023

cuda supports recursive __device__ functions on devices compute capability (cc) 2.0 , later. need verify gpu has cc 2.0 or later , compile -arch=sm_20 (or higher).

__global__ kernel functions can launched other kernels using cuda dynamic parallelism, requires cc >= 3.5

in case, performance reasons want make non-recursive version.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -