diff --git a/tensor_t/src/tensor_t/cl_tensor_t.c b/tensor_t/src/tensor_t/cl_tensor_t.c index a038cac..255b976 100644 --- a/tensor_t/src/tensor_t/cl_tensor_t.c +++ b/tensor_t/src/tensor_t/cl_tensor_t.c @@ -3,6 +3,12 @@ #define MAX_SOURCE_SIZE (0x100000) +#define FREE_dM_S_\ + free_dimension(dM0);\ + free_dimension(dM1);\ + free_dimension(dM);\ + free_dimension(dSub0);\ + free_dimension(dSub1);\ #define SETUP_cl_KERNEL_(type,file_cl_src,func_cl_name)\ @@ -18,7 +24,7 @@ fprintf(stderr, "Failed to load kernel. \n"); \ exit(1); \ } \ - source_str = (char*)malloc(MAX_SOURCE_SIZE); \ + source_str = malloc(MAX_SOURCE_SIZE); \ source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp); \ fclose( fp ); \ \ @@ -85,10 +91,11 @@ char *log = malloc(sizeof(char)*len);\ clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, len, log, NULL);\ printf("Error: Failed to build program executable!\n %s \n",log);\ + free(log);\ }\ /*/ Create the OpenCL kernel */ \ /*char func_cl_name[250]; sprintf(func_cl_name,"prodTensorLin_%s", #type);*/ \ - printf("cl_func_type = %s\n",func_cl_name); \ + /*printf("cl_func_type = %s\n",func_cl_name); */ \ cl_kernel kernel = clCreateKernel(program, func_cl_name, &ret); \ @@ -194,7 +201,7 @@ void cl_tensorContractnProd_##type(tensor_##type** MM, tensor_##type *M0, tensor dimension *dM1 = init_dim(tDk1, contractionNumber);\ dimension *dM0 = init_dim(tDk0, contractionNumber);\ dimension *dM;\ - min_dimension(&dM, dM0, dM1);\ + min_copy_dimension(&dM, dM0, dM1);\ \ dimension *dd;\ add_dimension(&dd, dSub0, dSub1);\ @@ -230,6 +237,7 @@ void cl_tensorContractnProd_##type(tensor_##type** MM, tensor_##type *M0, tensor EXEC_cl_KERNEL(type);\ READ_BUF_N_CLEANUP(type)\ \ + FREE_dM_S_ \ } \ \ \ @@ -283,7 +291,7 @@ void cl2d_tensorContractnProd_##type(tensor_##type **MM, tensor_##type *M0, tens dimension *dM1 = init_dim(tDk1, contractionNumber);\ dimension *dM0 = init_dim(tDk0, contractionNumber);\ dimension *dM;\ - min_dimension(&dM, dM0, dM1);\ + min_copy_dimension(&dM, dM0, dM1);\ \ dimension *dd;\ add_dimension(&dd, dSub0, dSub1);\ @@ -320,7 +328,8 @@ void cl2d_tensorContractnProd_##type(tensor_##type **MM, tensor_##type *M0, tens /*printf("EXEC_cl_2d_KERNEL(type,%ld,%ld,%ld,%ld)\n",dSub0->rank,dSub1->rank,div0Wsz,div1Wsz);\ */EXEC_cl_2d_KERNEL(type,dSub0->rank,dSub1->rank,div0Wsz,div1Wsz);\ READ_BUF_N_CLEANUP(type)\ -\ + FREE_dM_S_ \ + \ } \ \ diff --git a/tensor_t/src/tensor_t/tensor_t.c b/tensor_t/src/tensor_t/tensor_t.c index 08f7da8..5fa242b 100644 --- a/tensor_t/src/tensor_t/tensor_t.c +++ b/tensor_t/src/tensor_t/tensor_t.c @@ -506,6 +506,7 @@ void tensorProdThrea2d_##type(tensor_##type **MM, tensor_##type *M0, tensor_##ty free(thrd);\ free(arg_th);\ } \ +\ struct arg_ProdContract_##type{\ type *M0x;\ type *M1x;\ @@ -603,6 +604,98 @@ void tensorContractnProdThread_##type(tensor_##type** MM, tensor_##type *M0, ten free(arg_th);\ FREE_dM_S_ ; \ }\ +\ +struct arg_Pro2dContract_##type{\ + type *M0x;\ + type *M1x;\ + type *Mx;\ + size_t beginRange;\ + size_t endRange;\ + size_t dMRank;\ + size_t dSub0Rank;\ + size_t dSub1Rank;\ +};\ +void* runPro2dContract_thread_##type(void *arg){\ + struct arg_Pro2dContract_##type *arg_t = arg;\ + size_t n0_id, n1_id, l;\ + for (size_t i = arg_t->beginRange; i < arg_t->endRange; i++) {\ + for (size_t j = 0; j < arg_t->dSub1Rank; j++) {\ + if(endian)\ + l = j + arg_t->dSub1Rank * i;\ + else\ + l = j * arg_t->dMRank + i;\ + arg_t->Mx[l] = 0;\ + for (size_t k = 0; k < arg_t->dMRank; k++) {\ + if(endian){\ + n0_id= i * arg_t->dMRank + k;\ + n1_id= j + arg_t->dSub1Rank * k;\ + }\ + else{\ + n0_id= i + arg_t->dSub0Rank * k;\ + n1_id= j * arg_t->dMRank + k;\ + }\ + arg_t->Mx[l] += arg_t->M0x[n0_id] * arg_t->M1x[n1_id];\ + }\ + }\ + }\ +}\ +/* M[x0,x1,x3..xn] X M[y0,y1,y3..ym] = M[z0,z1...zp] (deep = l > 0) /exists 1<= l<...=n-l alor p=n+m-2l\ + M[x0,x1,x3..xl x{l+1}...xn] X M[xn,x{n-1},x{n-2}...xl y{l+1} ..ym] = M[x0,x1..xly{l+1}...y{n+m-2l}] (deep = l > 0)\ +M[[i][j]]=sum_{[k]}M0[[i][k]]*M[[k][j]]*/\ +\ +void tensorContractnPro2dThread_##type(tensor_##type** MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber, size_t nbthread) {\ +\ + size_t len0 = M0->dim->size - contractionNumber;\ + size_t len1 = M1->dim->size - contractionNumber;\ +\ + size_t* tsub0 = malloc(sizeof(size_t) *len0);\ + size_t* tsub1 = malloc(sizeof(size_t) *len1);\ + size_t* tDk1 = malloc(sizeof(size_t) *contractionNumber);\ + size_t* tDk0 = malloc(sizeof(size_t) *contractionNumber);\ + subArray(tsub0, M0->dim->perm, 0, len0, 0);\ + subArray(tsub1, M1->dim->perm, 0, len1, contractionNumber);\ + subArray(tDk1, M1->dim->perm, 0, contractionNumber, 0);\ + subArray(tDk0, M0->dim->perm, 0, contractionNumber, len0);\ + dimension *dSub0 = init_dim(tsub0, len0);\ + dimension *dSub1 = init_dim(tsub1, len1);\ + dimension *dM1 = init_dim(tDk1, contractionNumber);\ + dimension *dM0 = init_dim(tDk0, contractionNumber);\ + dimension *dM;\ + min_copy_dimension(&dM, dM0, dM1);\ + \ + dimension *dd;\ + add_dimension(&dd, dSub0, dSub1);\ + updateRankDim(dd);\ + *MM = CREATE_TENSOR_##type(dd);\ + tensor_##type *M= *MM;\ +\ +\ + \ + pthread_t *thrd = malloc(nbthread * sizeof(pthread_t));\ + struct arg_Pro2dContract_##type **arg_th = malloc( nbthread * sizeof(struct arg_Pro2dContract_##type *));\ +\ + for(size_t i = 0; i < nbthread; ++i) {\ + arg_th[i] = malloc(sizeof(struct arg_Pro2dContract_##type));\ + arg_th[i]->M0x=M0->x;\ + arg_th[i]->M1x=M1->x;\ + arg_th[i]->Mx=M->x;\ + arg_th[i]->beginRange = i*(dSub0->rank)/nbthread ;\ + arg_th[i]->endRange = (i+1)*(dSub0->rank)/nbthread ;\ + arg_th[i]->dSub1Rank = dSub1->rank;\ + arg_th[i]->dSub0Rank = dSub0->rank;\ + arg_th[i]->dMRank = dM->rank;\ + pthread_create(&thrd[i], NULL, runPro2dContract_thread_##type, (void*)arg_th[i]);\ + }\ +\ + for(size_t i=0; i< nbthread; ++i){\ + pthread_join(thrd[i], NULL);\ + free(arg_th[i]);\ + }\ +\ + free(thrd);\ + free(arg_th);\ + FREE_dM_S_ ; \ +}\ void tensorContractnProdNotOpt_##type(tensor_##type** MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber) {\ /* if (!checkMatchProdtensor(M0->dim, M1->dim, contractionNumber)) {\ prsize_tf("Deep = %d\n", contractionNumber);\ diff --git a/tensor_t/src/tensor_t/tensor_t.h b/tensor_t/src/tensor_t/tensor_t.h index 112d139..0e795a8 100644 --- a/tensor_t/src/tensor_t/tensor_t.h +++ b/tensor_t/src/tensor_t/tensor_t.h @@ -29,6 +29,7 @@ void tensorContractnProd_##type(tensor_##type **MM, tensor_##type *M0, tensor_## void tensorProdThread_##type(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1,size_t nbthread); \ void tensorProdThrea2d_##type(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1,size_t nbthread); \ void tensorContractnProdThread_##type(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber, size_t nbthread); \ +void tensorContractnPro2dThread_##type(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber, size_t nbthread); \ void tensorContractnProdNotOpt_##type(tensor_##type **MM, tensor_##type *M0, tensor_##type *M1, size_t contractionNumber); \ diff --git a/tensor_t/test/is_good.c b/tensor_t/test/is_good.c index 3271a98..a497046 100644 --- a/tensor_t/test/is_good.c +++ b/tensor_t/test/is_good.c @@ -17,7 +17,7 @@ //#include "permutation_t/permutation_t.h" #include "tensor_t/tensor_t.h" -#define VALGRING_ 1 +#define VALGRIND_ 0 TEST(rank){ dimension *D=create_dim(4); @@ -40,7 +40,7 @@ void print_tensor_float(tensor_TYPE_FLOAT *M, char *msg){ } LOG("================= %s ===============\n",msg); -#if VALGRING_ +#if VALGRIND_ for(size_t i=0; idim->rank;++i) LOG("[%ld]: %f ",i,M->x[i]); @@ -51,7 +51,7 @@ void print_tensor_float(tensor_TYPE_FLOAT *M, char *msg){ void print_tensor_double(tensor_TYPE_DOUBLE *M, char *msg){ LOG("================= %s ===============\n",msg); -#if VALGRING_ +#if VALGRIND_ for(size_t i=0; idim->rank;++i) LOG("[%ld]: %lf ",i,M->x[i]); @@ -262,7 +262,7 @@ TEST(tensorSubtail ){ TEST(tensorProd ){ dimension *d0=create_dim(3); dimension *d1=create_dim(2); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=5; d0->perm[1]=2; //3; d0->perm[2]=3; @@ -315,7 +315,7 @@ TEST(tensorProd ){ TEST(tensorContractnProd_TYPE_FLOAT ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=5; d0->perm[1]=2; //3; d0->perm[2]=3; @@ -377,7 +377,7 @@ TEST(tensorContractnProd_TYPE_FLOAT ){ TEST(tensorContractnProd_TYPE_FLOAT2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=5; d0->perm[1]=2; //3; d0->perm[2]=3; @@ -438,7 +438,7 @@ TEST(tensorContractnProd_TYPE_FLOAT2 ){ TEST(tensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=5; d0->perm[1]=2; //3; d0->perm[2]=3; @@ -499,7 +499,7 @@ TEST(tensorContractnProd_TYPE_DOUBLE2 ){ TEST(VStensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=5; d0->perm[1]=2; //3; d0->perm[2]=3; @@ -555,11 +555,72 @@ TEST(VStensorContractnProd_TYPE_DOUBLE2 ){ free_tensor_TYPE_DOUBLE(M1); } +TEST(Pthread_tensorContractnPro2d_TYPE_DOUBLE2 ){ + dimension *d0=create_dim(3); + dimension *d1=create_dim(3); +#if VALGRIND_ + d0->perm[0]=5; + d0->perm[1]=2; //3; + d0->perm[2]=3; + d1->perm[0]=2; + d1->perm[1]=3;//3; + d1->perm[2]=8; + +#else + + d0->perm[0]=125; + d0->perm[1]=52; //3; + d0->perm[2]=63; + + d1->perm[0]=52; + d1->perm[1]=63;//3; + d1->perm[2]=154; +#endif + + + updateRankDim(d0); + updateRankDim(d1); + + + tensor_TYPE_DOUBLE *M0 = CREATE_TENSOR_TYPE_DOUBLE(d0); + tensor_TYPE_DOUBLE *M1 = CREATE_TENSOR_TYPE_DOUBLE(d1); + + LOG("M0->dim->rank = %ld\n",M0->dim->rank); + LOG("M1->dim->rank = %ld\n",M1->dim->rank); + for(size_t i=0; idim->rank;++i) M0->x[i]=i*0.1 +1; + for(size_t i=0; idim->rank;++i) M1->x[i]=i*0.003 + 2; + + //print_tensor_double(M0,"M0"); + //print_tensor_double(M1,"M1"); + + tensor_TYPE_DOUBLE *M; + tensor_TYPE_DOUBLE *MnO; + + size_t nbthread = 5; + + tensorContractnProd_TYPE_DOUBLE(&M, M0,M1,2); + //print_tensor_double(M,"M"); + //cl_tensorContractnProd_TYPE_DOUBLE(&MnO, M0,M1,2); + tensorContractnPro2dThread_TYPE_DOUBLE(&MnO, M0,M1,2,nbthread); + + //print_tensor_double(MnO,"MnO"); + + // for(size_t i=0;idim->rank;++i) + // EXPECT_EQ_TYPE_DOUBLE(M->x[i],MnO->x[i]); + + EXPECT_ARRAY_EQ_TYPE_DOUBLE(M->x,M->dim->rank,MnO->x,MnO->dim->rank); + + free_tensor_TYPE_DOUBLE(M); + free_tensor_TYPE_DOUBLE(MnO); + free_tensor_TYPE_DOUBLE(M0); + free_tensor_TYPE_DOUBLE(M1); + +} TEST(Pthread_tensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=5; d0->perm[1]=2; //3; d0->perm[2]=3; @@ -624,7 +685,7 @@ TEST(tensorProd_vs ){ dimension *d1=create_dim(2); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=2; d0->perm[1]=3; d0->perm[2]=2; @@ -676,7 +737,7 @@ TEST(tensorProd_vsThread ){ dimension *d0=create_dim(3); dimension *d1=create_dim(2); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=2; d0->perm[1]=3; d0->perm[2]=2; @@ -734,7 +795,7 @@ TEST(tensorProd_vsThread2d ){ dimension *d0=create_dim(3); dimension *d1=create_dim(2); -#if VALGRING_ +#if VALGRIND_ d0->perm[0]=2; d0->perm[1]=3; d0->perm[2]=2; diff --git a/tensor_t/test_cl/is_good.c b/tensor_t/test_cl/is_good.c index 902c7bb..d2cbb5f 100644 --- a/tensor_t/test_cl/is_good.c +++ b/tensor_t/test_cl/is_good.c @@ -19,6 +19,7 @@ #include "tensor_t/cl_tensor_t.h" //#include "tools_t/tools_t.h" +#define VALGRIND 1 TEST(rank){ dimension *D=create_dim(4); @@ -36,19 +37,22 @@ TEST(rank){ void print_tensor_float(tensor_TYPE_FLOAT *M, char *msg){ LOG("================= %s ===============\n",msg); +#if VALGRIND for(size_t i=0; idim->rank;++i) LOG("[%ld]: %f ",i,M->x[i]); - LOG("%s","\n"); +#endif } void print_tensor_double(tensor_TYPE_DOUBLE *M, char *msg){ LOG("================= %s ===============\n",msg); +#if VALGRIND for(size_t i=0; idim->rank;++i) LOG("[%ld]: %lf ",i,M->x[i]); LOG("%s","\n"); +#endif } extern long int PRECISION_TYPE_FLOAT ; @@ -302,7 +306,17 @@ TEST(cl_tensorContractnProd_TYPE_DOUBLE2 ){ TEST(tensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); +#if VALGRIND + d0->perm[0]=12; + d0->perm[1]=5; //3; + d0->perm[2]=6; + + d1->perm[0]=5; + d1->perm[1]=6;//3; + d1->perm[2]=14; + +#else d0->perm[0]=125; d0->perm[1]=52; //3; d0->perm[2]=63; @@ -310,7 +324,7 @@ TEST(tensorContractnProd_TYPE_DOUBLE2 ){ d1->perm[0]=52; d1->perm[1]=63;//3; d1->perm[2]=54; - +#endif updateRankDim(d0); updateRankDim(d1); @@ -352,6 +366,7 @@ TEST(TensorProdCL){ dimension *d0=create_dim(3); dimension *d1=create_dim(2); + d0->perm[0]=2; d0->perm[1]=3; d0->perm[2]=2; @@ -375,6 +390,7 @@ TEST(TensorProdCL){ print_tensor_float(M1,"M1"); + tensor_TYPE_FLOAT *M; tensor_TYPE_FLOAT *Mn; @@ -399,7 +415,17 @@ TEST(TensorProdCL){ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); +#if VALGRIND + d0->perm[0]=12; + d0->perm[1]=4; //3; + d0->perm[2]=6; + + d1->perm[0]=4; + d1->perm[1]=6;//3; + d1->perm[2]=16; + +#else d0->perm[0]=512; d0->perm[1]=48; //3; @@ -408,6 +434,7 @@ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ d1->perm[0]=48; d1->perm[1]=64;//3; d1->perm[2]=240; +#endif updateRankDim(d0); updateRankDim(d1); @@ -457,7 +484,17 @@ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); +#if VALGRIND + d0->perm[0]=12; + d0->perm[1]=4; //3; + d0->perm[2]=6; + + d1->perm[0]=4; + d1->perm[1]=6;//3; + d1->perm[2]=16; + +#else d0->perm[0]=512; d0->perm[1]=48; //3; @@ -466,6 +503,9 @@ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ d1->perm[0]=48; d1->perm[1]=64;//3; d1->perm[2]=240; +#endif + + updateRankDim(d0); updateRankDim(d1); @@ -519,7 +559,17 @@ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); +#if VALGRIND + d0->perm[0]=12; + d0->perm[1]=4; //3; + d0->perm[2]=6; + + d1->perm[0]=4; + d1->perm[1]=6;//3; + d1->perm[2]=16; + +#else d0->perm[0]=512; d0->perm[1]=48; //3; @@ -528,6 +578,9 @@ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ d1->perm[0]=48; d1->perm[1]=64;//3; d1->perm[2]=240; +#endif + + updateRankDim(d0); updateRankDim(d1); @@ -578,13 +631,24 @@ TEST(VS_thrd_tensorContractnProd_TYPE_DOUBLE2 ){ -#if 1 TEST(VScltensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); +#if VALGRIND + + d0->perm[0]=12; + d0->perm[1]=4; //3; + d0->perm[2]=6; + + d1->perm[0]=4; + d1->perm[1]=6;//3; + d1->perm[2]=16; + +#else + d0->perm[0]=512; d0->perm[1]=48; //3; d0->perm[2]=64; @@ -592,6 +656,8 @@ TEST(VScltensorContractnProd_TYPE_DOUBLE2 ){ d1->perm[0]=48; d1->perm[1]=64;//3; d1->perm[2]=240; +#endif + updateRankDim(d0); updateRankDim(d1); @@ -636,6 +702,27 @@ TEST(VScltensorContractnProd_TYPE_DOUBLE2 ){ TEST(VScl2dtensorContractnProd_TYPE_DOUBLE2 ){ dimension *d0=create_dim(3); dimension *d1=create_dim(3); +#if VALGRIND + + d0->perm[0]=12; + d0->perm[1]=4; //3; + d0->perm[2]=6; + + d1->perm[0]=4; + d1->perm[1]=6;//3; + d1->perm[2]=16; + +#else + + d0->perm[0]=512; + d0->perm[1]=48; //3; + d0->perm[2]=64; + + d1->perm[0]=48; + d1->perm[1]=64;//3; + d1->perm[2]=240; +#endif + d0->perm[0]=512; d0->perm[1]=48; //3; @@ -876,7 +963,6 @@ TEST(tensorProd_vs2d_Endian ){ free_tensor_TYPE_FLOAT(M1); } -#endif int main(int argc, char **argv){ diff --git a/ytest_t/Makefile b/ytest_t/Makefile index 0587831..b47a7b7 100644 --- a/ytest_t/Makefile +++ b/ytest_t/Makefile @@ -40,7 +40,7 @@ $(SUBDIRS): $(MAKE) -C $@ $(MAKECMDGOALS) update_headers: $(PROJECT_LIB) - for file_h in $(SUBDIRS); do cp -r "$$file_h/include" include_ytest/; done + for file_h in $(SUBDIRS); do cp -r "$$file_h/include/" include_ytest/; done .PHONY: $(TOPTARGETS) $(SUBDIRS) diff --git a/ytest_t/include_ytest/include/fmock/fmock.h b/ytest_t/include_ytest/include/fmock/fmock.h index 6326ab9..d459ca2 100644 --- a/ytest_t/include_ytest/include/fmock/fmock.h +++ b/ytest_t/include_ytest/include/fmock/fmock.h @@ -24,6 +24,7 @@ struct list_current_variable{ */ struct func_mock_info_struct{ long id; + bool used; char *str_namefunc; char *str_conditions; char *str_caller; @@ -74,6 +75,7 @@ extern struct list_base_fmock *g_list_base_fmock; (tmp__mock)->call_mock_condition = NULL;\ /*(tmp__mock)->str_print_current_variables = list_mo_ ## namefunction .str_print_current_variables;*/\ ((tmp__mock)->info_mock)->expect_call = -1;\ + ((tmp__mock)->info_mock)->used = true;\ ((tmp__mock)->info_mock)->call = 0;\ ((tmp__mock)->info_mock)->failed_call = 0;\ ((tmp__mock)->info_mock)->str_namefunc = malloc(strlen(#namefunction) + 43 + strlen(#pre_id));\ @@ -101,6 +103,7 @@ extern struct list_base_fmock *g_list_base_fmock; } list_mo_ ## namefunction;\ __attribute__((constructor)) void init_list_m_ ## namefunction(void){\ list_mo_ ## namefunction.info_mock = malloc(sizeof(struct func_mock_info_struct));\ + (list_mo_ ## namefunction.info_mock)->used = false;\ (list_mo_ ## namefunction.info_mock)->times_left = INITSTATE;\ (list_mo_ ## namefunction.info_mock)->init_times_left = INITSTATE;\ list_mo_ ## namefunction.str_print_current_variables = NULL;\ @@ -110,12 +113,16 @@ extern struct list_base_fmock *g_list_base_fmock; __attribute__((destructor)) void destruct_list_m_ ## namefunction(void){ \ /*free(list_mo_ ## namefunction.info_mock);*/ \ struct list_mock_return_ ## namefunction *tmp_nn = list_mo_ ## namefunction.next, *ttmp_nn;\ + if((list_mo_ ## namefunction.info_mock)->used == false ){\ + free(list_mo_ ## namefunction.info_mock);\ + }\ while(tmp_nn){\ ttmp_nn = tmp_nn;\ tmp_nn = tmp_nn->next;\ /*free(ttmp_nn->info_mock);*/\ free(ttmp_nn);\ }\ + PRINT_DEBUG(" purge list mo_ %s done!\n",#namefunction);\ \ }\ \ @@ -248,6 +255,7 @@ __attribute__((constructor)) void create_str_print_variables ## namefunction(){\ (tmp_new_mock)->str_print_current_variables = list_mo_ ## namefunction .str_print_current_variables;\ /*(tmp_new_mock)->info_mock = malloc(sizeof(struct func_mock_info_struct));*/\ ((tmp_new_mock)->info_mock)->expect_call = f_expect_call;\ + ((tmp_new_mock)->info_mock)->used = true;\ ((tmp_new_mock)->info_mock)->call = 0;\ ((tmp_new_mock)->info_mock)->failed_call = 0;\ ((tmp_new_mock)->info_mock)->init_times_left = repeat;\ diff --git a/ytest_t/libytest.so b/ytest_t/libytest.so index c5c6b5d..24b2d2d 100644 Binary files a/ytest_t/libytest.so and b/ytest_t/libytest.so differ diff --git a/ytest_t/test/is_good.c b/ytest_t/test/is_good.c index bad9b2a..9f01353 100644 --- a/ytest_t/test/is_good.c +++ b/ytest_t/test/is_good.c @@ -13,11 +13,13 @@ #include "fmock/fmock.h" #if 1 + TEST(true__){ PRINTF("another test again false\n"); bool val_bool = false; ASSERT_TRUE(val_bool); } + TEST(test) { PRINTF("test test\n"); @@ -25,6 +27,7 @@ TEST(test) ASSERT_FALSE(val_bool); } + TEST(float_equal){ PRINTF("another test float\n"); ASSERT_TRUE(true); @@ -184,6 +187,8 @@ TEST(mockf1){ #endif + + #if 1 MOCK_FUNC(int, f2_mock,(int a,int b),(a,b)) @@ -349,6 +354,7 @@ TEST(f7_mock_test){ #endif + int main(int argc, char **argv){ //run_all_tests(); diff --git a/ytest_t/yfmock/include/fmock/fmock.h b/ytest_t/yfmock/include/fmock/fmock.h index 6326ab9..d459ca2 100644 --- a/ytest_t/yfmock/include/fmock/fmock.h +++ b/ytest_t/yfmock/include/fmock/fmock.h @@ -24,6 +24,7 @@ struct list_current_variable{ */ struct func_mock_info_struct{ long id; + bool used; char *str_namefunc; char *str_conditions; char *str_caller; @@ -74,6 +75,7 @@ extern struct list_base_fmock *g_list_base_fmock; (tmp__mock)->call_mock_condition = NULL;\ /*(tmp__mock)->str_print_current_variables = list_mo_ ## namefunction .str_print_current_variables;*/\ ((tmp__mock)->info_mock)->expect_call = -1;\ + ((tmp__mock)->info_mock)->used = true;\ ((tmp__mock)->info_mock)->call = 0;\ ((tmp__mock)->info_mock)->failed_call = 0;\ ((tmp__mock)->info_mock)->str_namefunc = malloc(strlen(#namefunction) + 43 + strlen(#pre_id));\ @@ -101,6 +103,7 @@ extern struct list_base_fmock *g_list_base_fmock; } list_mo_ ## namefunction;\ __attribute__((constructor)) void init_list_m_ ## namefunction(void){\ list_mo_ ## namefunction.info_mock = malloc(sizeof(struct func_mock_info_struct));\ + (list_mo_ ## namefunction.info_mock)->used = false;\ (list_mo_ ## namefunction.info_mock)->times_left = INITSTATE;\ (list_mo_ ## namefunction.info_mock)->init_times_left = INITSTATE;\ list_mo_ ## namefunction.str_print_current_variables = NULL;\ @@ -110,12 +113,16 @@ extern struct list_base_fmock *g_list_base_fmock; __attribute__((destructor)) void destruct_list_m_ ## namefunction(void){ \ /*free(list_mo_ ## namefunction.info_mock);*/ \ struct list_mock_return_ ## namefunction *tmp_nn = list_mo_ ## namefunction.next, *ttmp_nn;\ + if((list_mo_ ## namefunction.info_mock)->used == false ){\ + free(list_mo_ ## namefunction.info_mock);\ + }\ while(tmp_nn){\ ttmp_nn = tmp_nn;\ tmp_nn = tmp_nn->next;\ /*free(ttmp_nn->info_mock);*/\ free(ttmp_nn);\ }\ + PRINT_DEBUG(" purge list mo_ %s done!\n",#namefunction);\ \ }\ \ @@ -248,6 +255,7 @@ __attribute__((constructor)) void create_str_print_variables ## namefunction(){\ (tmp_new_mock)->str_print_current_variables = list_mo_ ## namefunction .str_print_current_variables;\ /*(tmp_new_mock)->info_mock = malloc(sizeof(struct func_mock_info_struct));*/\ ((tmp_new_mock)->info_mock)->expect_call = f_expect_call;\ + ((tmp_new_mock)->info_mock)->used = true;\ ((tmp_new_mock)->info_mock)->call = 0;\ ((tmp_new_mock)->info_mock)->failed_call = 0;\ ((tmp_new_mock)->info_mock)->init_times_left = repeat;\ diff --git a/ytest_t/yfmock/src/fmock/fmock.c b/ytest_t/yfmock/src/fmock/fmock.c index b58f736..b7326a0 100644 --- a/ytest_t/yfmock/src/fmock/fmock.c +++ b/ytest_t/yfmock/src/fmock/fmock.c @@ -34,7 +34,6 @@ pthread_mutex_t mut_g_list_base_fmock; * return the numbers of comas (,) +1 in the input string */ - int parse_count_args_(char *input){ int val=0; for(int i = 0; i< strlen(input); ++i){ @@ -90,6 +89,7 @@ void append_variable_current(struct list_current_variable **lcurrent_var, char * } } + void append_fmock_to_listmock(struct func_mock_info_struct **f_mock_list, struct func_mock_info_struct *f_mock){ INCREMENT_(count_f_mock_wished); if(f_mock->expect_call) { @@ -140,7 +140,6 @@ __attribute__((constructor)) - void clear_fmock_info_list(struct func_mock_info_struct **f_mock_list){ if(*f_mock_list){ struct func_mock_info_struct *tmp_fmock_info = *f_mock_list, *ttmp_fmock_info; @@ -155,7 +154,6 @@ void clear_fmock_info_list(struct func_mock_info_struct **f_mock_list){ } *f_mock_list = NULL; } - } void clear_list_base_fmock(struct list_base_fmock **l_fmock){ @@ -187,6 +185,7 @@ void clear_variable_current(struct list_current_variable **lcurrent_var){ } +/* destructor will be executed in inverse order of call/place in the file */ __attribute__((destructor)) void purge_fmock(){ @@ -197,11 +196,9 @@ __attribute__((destructor)) } - extern bool is_parallel_nb; - char * number_call_translate(long nb){ char *ret=malloc(250); if(nb>1) sprintf(ret," be called %ld times",nb); @@ -234,7 +231,6 @@ char * strprint_caller_(char *input){ PRINTF("\n");\ }while(0); - __attribute__((destructor)) @@ -256,7 +252,8 @@ __attribute__((destructor)) char *reader=malloc(w.ws_col+3); strcpy(reader,"STAT OF MOCK FUNCTIONS"); - fprintf(F_OUT,"%s\n\n%0*d\n %*s \n%0*d %s\n\n", colors_f[k_YELLOW] ,w.ws_col,0, (int)(w.ws_col+strlen(reader))/2, reader,w.ws_col,0, DEFAULT_K ); + //fprintf(F_OUT,"%s\n\n%0*d\n %*s \n%0*d %s\n\n", colors_f[k_YELLOW] ,w.ws_col,0, (int)(w.ws_col+strlen(reader))/2, reader,w.ws_col,0, DEFAULT_K ); + PRINTF("%s\n\n%0*d\n %*s \n%0*d %s\n\n", colors_f[k_YELLOW] ,w.ws_col,0, (int)(w.ws_col+strlen(reader))/2, reader,w.ws_col,0, DEFAULT_K ); is_parallel_nb = 0; /* no longer parallel here */ @@ -417,6 +414,5 @@ __attribute__((destructor)) PRINT_DEBUG("%s\n","pthread_mutex_destroy done!"); PRINT_DEBUG("%s\n","check mock done!"); - } diff --git a/ytest_t/yftest/src/ftest/ftest.c b/ytest_t/yftest/src/ftest/ftest.c index a63302b..2f4e0b0 100644 --- a/ytest_t/yftest/src/ftest/ftest.c +++ b/ytest_t/yftest/src/ftest/ftest.c @@ -213,7 +213,11 @@ long int id_of_thread_executed(void){ if(id_thread_self[i] == id_from_self) return i; } - PRINT_ERROR("something wrong on %s, id_from_self: %ld\n",__func__,id_from_self); + /*if(id_thread_self){ + for(long int i=0; i<= parallel_nb; ++i) + PRINT_DEBUG(" id_thread_self[%ld] = %ld \n", i, id_thread_self[i]); + }*/ + PRINT_ERROR("\nsomething wrong on %s, id_from_self: %ld\n",__func__,id_from_self); return -1; } @@ -1007,10 +1011,10 @@ stat_end_run(size_t ntst, struct timespec start_t){ bool is_in_array_##type(type *array, type val){\ bool found = false;\ for(size_t i = 0; i < cur_array_##type; ++i){\ - /*char * strarr = type##_TO_STR(array[i]), *strval = type##_TO_STR(val);\ + char * strarr = type##_TO_STR(array[i]), *strval = type##_TO_STR(val);\ PRINT_DEBUG("compare |%s| in array and val: |%s|\n",strarr, strval);\ free(strarr);free(strval);\ - */PRINT_DEBUG("compare |%s| in array and val: |%s|\n",type##_TO_STR(array[i]), type##_TO_STR(val));\ + /*PRINT_DEBUG("compare |%s| in array and val: |%s|\n",type##_TO_STR(array[i]), type##_TO_STR(val));*/\ if(COMPARE_N_##type((void*)(array[i]),(void*)val ) == 0 ){\ found = true;\ break;\ @@ -1024,10 +1028,10 @@ bool is_in_array_##type(type *array, type val){\ bool is_in_array_##type(type *array, type val){\ bool found = false;\ for(size_t i = 0; i < cur_array_##type; ++i){\ - /*char * strarr = type##_TO_STR(array[i]), *strval = type##_TO_STR(val);\ + char * strarr = type##_TO_STR(array[i]), *strval = type##_TO_STR(val);\ PRINT_DEBUG("compare |%s| in array and val: |%s|\n",strarr, strval);\ - free(strarr);free(strval);*/\ - PRINT_DEBUG("compare |%s| in array and val: |%s|\n",type##_TO_STR(array[i]), type##_TO_STR(val));\ + free(strarr);free(strval);\ + /*PRINT_DEBUG("compare |%s| in array and val: |%s|\n",type##_TO_STR(array[i]), type##_TO_STR(val));*/\ if(COMPARE_N_##type((void*)(&array[i]),(void*)&val ) == 0 ){\ found = true;\ break;\ @@ -1312,6 +1316,7 @@ init_parallel_test_() thread_test_failed_l[i] = NULL; count_pass_thread[i] = 0; count_fail_thread[i] = 0; + id_thread_self[i]=0; /* have to initialize because if some threads not yetr run we have warning with valgrind : non initialize value, beause real value is provide by each thread */ } current_fn = f_beging; @@ -1336,6 +1341,9 @@ final_parallel_test_() free(count_pass_thread); free(count_fail_thread); + free(id_thread_self); + //id_thread_self = NULL; + for(size_t i=0; i< parallel_nb; ++i) clear_all_falied_list(&thread_test_failed_l[i]); @@ -1377,17 +1385,20 @@ final_parallel_test_() fclose(f_ou_th[id_thrd]); } } - + + free(f_ou_th); + if(removelog){ for(size_t i=0; i<=parallel_nb; ++i){ remove(log_name_file_thrd[i]); PRINT_DEBUG("file log of treard[%ld] removed\n",i); - free(log_name_file_thrd[i]); } - free(log_name_file_thrd); } + for(size_t i=0; i<=parallel_nb; ++i) + free(log_name_file_thrd[i]); + free(log_name_file_thrd); } void run_all_tests_parallel(size_t parallel /*, int max_col*/)