1 /* This file is part of the Random Ball Cover (RBC) library.
2 * (C) Copyright 2010, Lawrence Cayton [lcayton@tuebingen.mpg.de]
3 */
5 #ifndef UTILS_CU
6 #define UTILS_CU
8 #include<sys/time.h>
9 #include<stdio.h>
10 #include "utils.h"
11 #include "defs.h"
12 #include "kernels.h"
14 //Returns a length l subset of a random permutation of [0,...,n-1]
15 //using the knuth shuffle.
16 //Input variable x is assumed to be alloced and of size l.
17 void subRandPerm(unint l, unint n, unint *x){
18 unint i,ri, *y;
19 y = (unint*)calloc(n,sizeof(*y));
21 struct timeval t3;
22 gettimeofday(&t3,NULL);
23 srand(t3.tv_usec);
25 for(i=0;i<n;i++)
26 y[i]=i;
28 for(i=0;i<MIN(l,n-1);i++){ //The n-1 bit is necessary because you can't swap the last
29 //element with something larger.
30 ri=randBetween(i+1,n);
31 swap(&y[i],&y[ri]);
32 }
34 for(i=0;i<l;i++)
35 x[i]=y[i];
36 free(y);
37 }
39 //Generates a random permutation of 0, ... , n-1 using the knuth shuffle.
40 //This should probably be merged with subRandPerm.
41 void randPerm(unint n, unint *x){
42 unint i,ri;
44 struct timeval t3;
45 gettimeofday(&t3,NULL);
46 srand(t3.tv_usec);
48 for(i=0;i<n;i++){
49 x[i]=i;
50 }
52 for(i=0;i<n-1;i++){
53 ri=randBetween(i+1,n);
54 swap(&x[i],&x[ri]);
55 }
56 }
58 void swap(unint *a, unint *b){
59 unint t;
60 t=*a; *a=*b; *b=t;
61 }
63 //generates a rand int in rand [a,b)
64 unint randBetween(unint a, unint b){
65 unint val,c;
67 if(b<=a){
68 fprintf(stderr,"misuse of randBetween.. exiting\n");
69 exit(1);
70 }
71 c= b-a;
73 while(c<= (val= rand()/(int)(((unsigned)RAND_MAX + 1) / c)));
74 val=val+a;
76 return val;
77 }
80 void printMat(matrix A){
81 unint i,j;
82 for(i=0;i<A.r;i++){
83 for(j=0;j<A.c;j++)
84 printf("%6.4f ",(float)A.mat[IDX(i,j,A.ld)]);
85 printf("\n");
86 }
87 }
90 void printMatWithIDs(matrix A, unint *id){
91 unint i,j;
92 for(i=0;i<A.r;i++){
93 for(j=0;j<A.c;j++)
94 printf("%6.4f ",(float)A.mat[IDX(i,j,A.ld)]);
95 printf("%d ",id[i]);
96 printf("\n");
97 }
98 }
101 void printCharMat(charMatrix A){
102 unint i,j;
103 for(i=0;i<A.r;i++){
104 for(j=0;j<A.c;j++)
105 printf("%d ",(char)A.mat[IDX(i,j,A.ld)]);
106 printf("\n");
107 }
108 }
110 void printIntMat(intMatrix A){
111 unint i,j;
112 for(i=0;i<A.r;i++){
113 for(j=0;j<A.c;j++)
114 printf("%d ",(unint)A.mat[IDX(i,j,A.ld)]);
115 printf("\n");
116 }
117 }
119 void printVector(real *x, unint d){
120 unint i;
122 for(i=0 ; i<d; i++)
123 printf("%6.2f ",x[i]);
124 printf("\n");
125 }
128 void copyVector(real *x, real *y, unint d){
129 unint i;
131 for(i=0;i<d;i++)
132 x[i]=y[i];
133 }
136 void copyMat(matrix *x, matrix *y){
137 unint i,j;
139 x->r=y->r; x->pr=y->pr; x->c=y->c; x->pc=y->pc; x->ld=y->ld;
140 for(i=0; i<y->r; i++){
141 for(j=0; j<y->c; j++){
142 x->mat[IDX( i, j, x->ld )] = y->mat[IDX( i, j, y->ld )];
143 }
144 }
145 }
148 void initMat(matrix *x, unint r, unint c){
149 x->r = r;
150 x->c = c;
154 }
157 void initIntMat(intMatrix *x, unint r, unint c){
158 x->r = r;
159 x->c = c;
163 }
165 // returns the size of a matrix in bytes
166 size_t sizeOfMatB(matrix x){
167 return x.pr*x.pc*sizeof(*x.mat);
168 }
170 size_t sizeOfIntMatB(intMatrix x){
171 return x.pr*x.pc*sizeof(*x.mat);
172 }
173 // returns the numbers of elements in a matrix
174 size_t sizeOfMat(matrix x){
175 return x.pr*x.pc;
176 }
178 size_t sizeOfIntMat(intMatrix x){
179 return x.pr*x.pc;
180 }
183 real distVec(matrix x, matrix y, unint k, unint l){
184 unint i;
185 real ans=0;
187 for(i=0;i<x.c;i++)
188 ans += DIST( x.mat[IDX(k,i,x.ld)], y.mat[IDX(l,i,x.ld)] );
189 //ans+=fabs(x.mat[IDX(k,i,x.ld)]-y.mat[IDX(l,i,x.ld)]);
190 return ans;
191 }
193 double timeDiff(struct timeval start, struct timeval end){
194 return (double)(end.tv_sec+end.tv_usec/1e6 - start.tv_sec - start.tv_usec/1e6);
195 }
199 #endif