2016-04-22 18:21:05 +01:00
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <ctype.h>
/*Program used to generate union2.cu and union2.h. A new pointer and all its operations are generated for each set (pairs, triplets, etc.).
Arguments are the cardinality of the biggest set and the name of the cuda file . For example , executing " creator2 20 union2 " will generate
all pointers and operations for all sets from 1 to 20 in the files union2 . cu and union2 . h . */
int main ( int argc , char * argv [ ] )
{
int num = atoi ( argv [ 1 ] ) ;
int x ;
char * str = ( char * ) malloc ( ( strlen ( argv [ 2 ] ) + 4 ) * sizeof ( char ) ) ;
sprintf ( str , " %s.cu " , argv [ 2 ] ) ;
FILE * cuda = fopen ( str , " w " ) ;
fprintf ( cuda , " /*Computer generated file to remove duplicates. Since Thrust's unique and sort, unlike their std's counterparts, don't have a way to specify the size of each element in \n " ) ;
fprintf ( cuda , " the array, comparing pairs, triplets and other sets is not possible without defining a new pointer and all related operations for each set. If you have a better idea to do \n " ) ;
fprintf ( cuda , " this, please don't hesitate to email us.*/ \n \n " ) ;
fprintf ( cuda , " #include <thrust/device_vector.h> \n " ) ;
fprintf ( cuda , " #include <thrust/unique.h> \n " ) ;
fprintf ( cuda , " #include <thrust/distance.h> \n " ) ;
fprintf ( cuda , " #include <thrust/sort.h> \n " ) ;
fprintf ( cuda , " #include <iostream> \n " ) ;
fprintf ( cuda , " #include \" memory.h \" \n " ) ;
fprintf ( cuda , " #include \" %s.h \" \n \n " , argv [ 2 ] ) ;
fprintf ( cuda , " int unir(int *res, int rows, int tipo, int **ret, int final) \n " ) ;
fprintf ( cuda , " { \n " ) ;
fprintf ( cuda , " \t thrust::device_ptr<int> pt, re; \n " ) ;
for ( x = 2 ; x < = num ; x + + )
fprintf ( cuda , " \t thrust::device_ptr<s%d> pt%d, re%d; \n " , x , x , x ) ;
for ( x = 2 ; x < = num ; x + + )
fprintf ( cuda , " \t s%d *t%d; \n " , x , x ) ;
fprintf ( cuda , " \t int flag, nrows, *nres, size; \n \n " ) ;
fprintf ( cuda , " #if TIMER \n " ) ;
fprintf ( cuda , " \t cuda_stats.unions++; \n " ) ;
fprintf ( cuda , " #endif \n \n " ) ;
fprintf ( cuda , " \t switch(tipo) \n " ) ;
fprintf ( cuda , " \t { \n " ) ;
fprintf ( cuda , " \t \t case 1: \n " ) ;
fprintf ( cuda , " \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t pt = thrust::device_pointer_cast(res); \n " ) ;
fprintf ( cuda , " \t \t \t flag = 0; \n " ) ;
fprintf ( cuda , " \t \t \t while(flag != 1) \n " ) ;
fprintf ( cuda , " \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t try \n " ) ;
fprintf ( cuda , " \t \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t \t thrust::sort(pt, pt + rows); \n " ) ;
fprintf ( cuda , " \t \t \t \t \t if(final) \n " ) ;
fprintf ( cuda , " \t \t \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t \t \t re = thrust::unique(pt, pt + rows, q1()); \n " ) ;
fprintf ( cuda , " \t \t \t \t \t \t re = thrust::unique(pt, re); \n " ) ;
fprintf ( cuda , " \t \t \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t \t \t else \n " ) ;
fprintf ( cuda , " \t \t \t \t \t \t re = thrust::unique(pt, pt + rows); \n " ) ;
fprintf ( cuda , " \t \t \t \t \t flag = 1; \n " ) ;
fprintf ( cuda , " \t \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t \t catch(std::bad_alloc &e) \n " ) ;
fprintf ( cuda , " \t \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t \t limpiar( \" sort/unique in unir \" , 0); \n " ) ;
fprintf ( cuda , " \t \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t nrows = thrust::distance(pt, re); \n " ) ;
fprintf ( cuda , " \t \t \t if(nrows < rows / 2) \n " ) ;
fprintf ( cuda , " \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t size = nrows * tipo * sizeof(int); \n " ) ;
fprintf ( cuda , " \t \t \t \t reservar(&nres, size); \n " ) ;
2016-07-31 16:14:02 +01:00
fprintf ( cuda , " \t \t \t \t cudaMemcpyAsync(nres, res, size, hipMemcpyDeviceToDevice); \n " ) ;
2016-04-22 18:21:05 +01:00
fprintf ( cuda , " \t \t \t \t cudaFree(*ret); \n " ) ;
fprintf ( cuda , " \t \t \t \t *ret = nres; \n " ) ;
fprintf ( cuda , " \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t return nrows; \n " ) ;
fprintf ( cuda , " \t \t } \n " ) ;
for ( x = 2 ; x < = num ; x + + )
{
fprintf ( cuda , " \t \t case %d: \n " , x ) ;
fprintf ( cuda , " \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t t%d = (s%d*)res; \n " , x , x ) ;
fprintf ( cuda , " \t \t \t pt%d = thrust::device_pointer_cast(t%d); \n " , x , x ) ;
fprintf ( cuda , " \t \t \t flag = 0; \n " ) ;
fprintf ( cuda , " \t \t \t while(flag != 1) \n " ) ;
fprintf ( cuda , " \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t try \n " ) ;
fprintf ( cuda , " \t \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t \t thrust::sort(pt%d, pt%d + rows, o%d()); \n " , x , x , x ) ;
fprintf ( cuda , " \t \t \t \t \t if(final) \n " ) ;
fprintf ( cuda , " \t \t \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t \t \t re%d = thrust::unique(pt%d, pt%d + rows, q%d()); \n " , x , x , x , x ) ;
fprintf ( cuda , " \t \t \t \t \t \t re%d = thrust::unique(pt%d, re%d, p%d()); \n " , x , x , x , x ) ;
fprintf ( cuda , " \t \t \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t \t \t else \n " ) ;
fprintf ( cuda , " \t \t \t \t \t \t re%d = thrust::unique(pt%d, pt%d + rows, p%d()); \n " , x , x , x , x ) ;
fprintf ( cuda , " \t \t \t \t \t flag = 1; \n " ) ;
fprintf ( cuda , " \t \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t \t catch(std::bad_alloc &e) \n " ) ;
fprintf ( cuda , " \t \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t \t limpiar( \" sort/unique in unir \" , 0); \n " ) ;
fprintf ( cuda , " \t \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t nrows = thrust::distance(pt%d, re%d); \n " , x , x ) ;
fprintf ( cuda , " \t \t \t if(nrows < rows / 2) \n " ) ;
fprintf ( cuda , " \t \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t \t size = nrows * tipo * sizeof(int); \n " ) ;
fprintf ( cuda , " \t \t \t \t reservar(&nres, size); \n " ) ;
2016-07-31 16:14:02 +01:00
fprintf ( cuda , " \t \t \t \t cudaMemcpyAsync(nres, res, size, hipMemcpyDeviceToDevice); \n " ) ;
2016-04-22 18:21:05 +01:00
fprintf ( cuda , " \t \t \t \t cudaFree(*ret); \n " ) ;
fprintf ( cuda , " \t \t \t \t *ret = nres; \n " ) ;
fprintf ( cuda , " \t \t \t } \n " ) ;
fprintf ( cuda , " \t \t \t return nrows; \n " ) ;
fprintf ( cuda , " \t \t } \n " ) ;
}
fprintf ( cuda , " \t } \n " ) ;
fprintf ( cuda , " \t return 0; \n " ) ;
fprintf ( cuda , " } \n " ) ;
fclose ( cuda ) ;
sprintf ( str , " %s.h " , argv [ 2 ] ) ;
cuda = fopen ( str , " w " ) ; /*tipo de archivo cambiar*/
fprintf ( cuda , " #ifndef _ " ) ;
for ( x = 0 ; x < strlen ( argv [ 2 ] ) ; x + + )
fprintf ( cuda , " %c " , toupper ( argv [ 2 ] [ x ] ) ) ;
fprintf ( cuda , " _H_ \n " ) ;
fprintf ( cuda , " #define _ " ) ;
for ( x = 0 ; x < strlen ( argv [ 2 ] ) ; x + + )
fprintf ( cuda , " %c " , toupper ( argv [ 2 ] [ x ] ) ) ;
fprintf ( cuda , " _H_ \n \n " ) ;
fprintf ( cuda , " int unir(int *res, int rows, int tipo, int **ret, int final); \n \n " ) ;
for ( x = 2 ; x < = num ; x + + )
{
fprintf ( cuda , " typedef struct n%d \n " , x ) ;
fprintf ( cuda , " { \n " ) ;
fprintf ( cuda , " \t int v[%d]; \n " , x ) ;
fprintf ( cuda , " }s%d; \n \n " , x ) ;
}
fprintf ( cuda , " struct q1 \n " ) ;
fprintf ( cuda , " { \n " ) ;
fprintf ( cuda , " \t __host__ __device__ \n " ) ;
fprintf ( cuda , " \t bool operator()(const int &r1, const int &r2) \n " ) ;
fprintf ( cuda , " \t { \n " ) ;
fprintf ( cuda , " \t \t if(r1 != r2) \n " ) ;
fprintf ( cuda , " \t \t \t return true; \n " ) ;
fprintf ( cuda , " \t \t return false; \n " ) ;
fprintf ( cuda , " \t } \n " ) ;
fprintf ( cuda , " }; \n \n " ) ;
for ( x = 2 ; x < = num ; x + + )
{
fprintf ( cuda , " struct p%d \n " , x ) ;
fprintf ( cuda , " { \n " ) ;
fprintf ( cuda , " \t __host__ __device__ \n " ) ;
fprintf ( cuda , " \t bool operator()(const s%d &r1, const s%d &r2) \n " , x , x ) ;
fprintf ( cuda , " \t { \n " ) ;
fprintf ( cuda , " \t \t int x; \n " ) ;
fprintf ( cuda , " \t \t for(x = 0; x < %d; x++) \n " , x ) ;
fprintf ( cuda , " \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t if(r1.v[x] != r2.v[x]) \n " ) ;
fprintf ( cuda , " \t \t \t \t return false; \n " ) ;
fprintf ( cuda , " \t \t } \n " ) ;
fprintf ( cuda , " \t \t return true; \n " ) ;
fprintf ( cuda , " \t } \n " ) ;
fprintf ( cuda , " }; \n \n " ) ;
fprintf ( cuda , " struct q%d \n " , x ) ;
fprintf ( cuda , " { \n " ) ;
fprintf ( cuda , " \t __host__ __device__ \n " ) ;
fprintf ( cuda , " \t bool operator()(const s%d &r1, const s%d &r2) \n " , x , x ) ;
fprintf ( cuda , " \t { \n " ) ;
fprintf ( cuda , " \t \t int x; \n " ) ;
fprintf ( cuda , " \t \t for(x = 0; x < %d; x++) \n " , x ) ;
fprintf ( cuda , " \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t if(r1.v[x] != r2.v[x]) \n " ) ;
fprintf ( cuda , " \t \t \t \t return true; \n " ) ;
fprintf ( cuda , " \t \t } \n " ) ;
fprintf ( cuda , " \t \t return false; \n " ) ;
fprintf ( cuda , " \t } \n " ) ;
fprintf ( cuda , " }; \n \n " ) ;
fprintf ( cuda , " struct o%d \n " , x ) ;
fprintf ( cuda , " { \n " ) ;
fprintf ( cuda , " \t __host__ __device__ \n " ) ;
fprintf ( cuda , " \t bool operator()(const s%d &r1, const s%d &r2) \n " , x , x ) ;
fprintf ( cuda , " \t { \n " ) ;
fprintf ( cuda , " \t \t int x; \n " ) ;
fprintf ( cuda , " \t \t for(x = 0; x < %d; x++) \n " , x ) ;
fprintf ( cuda , " \t \t { \n " ) ;
fprintf ( cuda , " \t \t \t if(r1.v[x] > r2.v[x]) \n " ) ;
fprintf ( cuda , " \t \t \t \t return true; \n " ) ;
fprintf ( cuda , " \t \t \t if(r1.v[x] < r2.v[x]) \n " ) ;
fprintf ( cuda , " \t \t \t \t return false; \n " ) ;
fprintf ( cuda , " \t \t } \n " ) ;
fprintf ( cuda , " \t \t return false; \n " ) ;
fprintf ( cuda , " \t } \n " ) ;
fprintf ( cuda , " }; \n \n " ) ;
}
fprintf ( cuda , " #endif \n " ) ;
fclose ( cuda ) ;
free ( str ) ;
}