1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
#include "debug.h"
#include "resampling.h"
#include <strings.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include "myrand.h"
struct chi2s {
int nb_leaves;
struct cc*leaves;
struct cc*th;
int nb_nodes;
int *stashed_nodes;
int *next_nodes;
struct cc*nodes;
};
static void compute_chi2s(const struct tree *tree, const struct cc *lcc,
struct chi2s *temp,
int prolonge, datatype_t *results)
{
int first_leaf=0;
int nb_next_nodes=0;
int nb_stashed_nodes;
assert(temp->nb_leaves==tree->nb_leaves);
assert(temp->nb_nodes==tree->nb_nodes);
bzero(temp->nodes, tree->nb_nodes*sizeof(struct cc));
bzero(temp->stashed_nodes, tree->nb_nodes*sizeof(int));
//debug("max_depth=%i, nb_leaves=%i, nb_nodes=%i",
// tree->max_depth, tree->nb_leaves, tree->nb_nodes);
int depth;
for (depth=tree->max_depth; depth>0; depth--){
int nb_leaves=0;
int next_first_leaf;
int i;
nb_stashed_nodes=0;
for(i=0; i<nb_next_nodes; i++) {
int id=temp->next_nodes[i];
temp->leaves[nb_leaves++]=temp->nodes[id];
int parent=tree->np[id];
if (parent != -1) {
if (!temp->stashed_nodes[parent]) {
temp->stashed_nodes[parent]=1;
temp->next_nodes[nb_stashed_nodes++]=parent;
}
temp->nodes[parent].cases+=temp->nodes[id].cases;
temp->nodes[parent].controls+=temp->nodes[id].controls;
}
}
for(i=first_leaf; i<tree->nb_leaves && tree->ld[i] == depth; i++) {
temp->leaves[nb_leaves++]=lcc[i];
int parent=tree->lp[i];
if (parent != -1) {
if (!temp->stashed_nodes[parent]) {
temp->stashed_nodes[parent]=1;
temp->next_nodes[nb_stashed_nodes++]=parent;
}
temp->nodes[parent].cases+=lcc[i].cases;
temp->nodes[parent].controls+=lcc[i].controls;
}
}
assert(nb_stashed_nodes <= tree->nb_leaves);
next_first_leaf=i;
if (prolonge == 1) {
for(; i<tree->nb_leaves; i++) {
temp->leaves[nb_leaves++]=lcc[i];
}
}
nb_next_nodes=nb_stashed_nodes;
first_leaf=next_first_leaf;
//debug("depth=%i, ddl=%i", depth, nb_leaves-1);
struct calcul_chi2_res r=calcul_chi2(nb_leaves, temp->leaves,
0, 0, temp->th);
assert(r.error==0);
results[depth-1]=r.chi2;
}
}
struct paractl {
volatile int permutation;
int nb_permutations;
const struct tree *tree;
const struct cc *lcc;
int cases;
int controls;
int prolonge;
datatype_t *results;
};
struct parainfo {
struct paractl *ctl;
int permutation;
};
struct memory {
struct cc *rand_lcc;
struct chi2s temp;
};
static struct memory *mem_alloc(const struct tree *tree)
{
struct memory *m=malloc(sizeof(struct memory));
m->rand_lcc=malloc(tree->nb_leaves*sizeof(struct cc));
m->temp=(struct chi2s){
.nb_leaves=tree->nb_leaves,
.leaves=malloc(tree->nb_leaves*sizeof(struct cc)),
.th=malloc(tree->nb_leaves*sizeof(struct cc)),
.nb_nodes=tree->nb_nodes,
.stashed_nodes=malloc(tree->nb_nodes*sizeof(int)),
.next_nodes=malloc(tree->nb_nodes*sizeof(int)),
.nodes=malloc(tree->nb_nodes*sizeof(struct cc)),
};
return m;
}
static void free_alloc(struct memory *m)
{
free(m->temp.leaves);
free(m->temp.th);
free(m->temp.stashed_nodes);
free(m->temp.next_nodes);
free(m->temp.nodes);
free(m->rand_lcc);
}
static void *resampling_worker(void* param) {
struct parainfo *p=(struct parainfo *)param;
/* Copy values for ro access, to avoid ping-pong with cache lines
when updating permutation */
struct paractl data= *(p->ctl);
int permutation=p->permutation;
struct memory *mem=mem_alloc(data.tree);
myrand_init(pthread_self()+getpid());
while (permutation < data.nb_permutations) {
//debug("thread %i handle perm %i", p->permutation, permutation);
random_clades(data.tree->nb_leaves, data.lcc,
data.cases, data.controls, mem->rand_lcc);
compute_chi2s(data.tree, mem->rand_lcc, &mem->temp, data.prolonge,
data.results+(permutation * data.tree->max_depth));
permutation=__sync_fetch_and_add(&p->ctl->permutation, 1);
}
free_alloc(mem);
return NULL;
}
/* From http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine */
static int nbproc()
{
int numCPU=0;
#ifdef __linux
numCPU = sysconf( _SC_NPROCESSORS_ONLN );
#elif defined(__bsd)
int mib[4];
size_t len = sizeof(numCPU);
/* set the mib for hw.ncpu */
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
/* get the number of CPUs from the system */
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if( numCPU < 1 ) {
mib[1] = HW_NCPU;
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
if( numCPU < 1 ) {
numCPU = 1;
}
}
#else
# warning no support on this plate-form. Patch welcome.
#endif
return numCPU;
}
int resampling_chi2(const struct tree *tree, const struct cc *lcc, int prolonge,
int nb_permutations, datatype_t *results, int parallel)
{
#if defined(DEBUG) && 0
FILE* dump=fopen("/tmp/dump", "w+");
fwrite(tree, sizeof(struct tree), 1, dump);
fwrite(tree->ld, sizeof(int), tree->nb_leaves, dump);
fwrite(tree->lp, sizeof(int), tree->nb_leaves, dump);
fwrite(tree->np, sizeof(int), tree->nb_nodes, dump);
fwrite(lcc, sizeof(struct cc), tree->nb_leaves, dump);
fwrite(&prolonge, sizeof(int), 1, dump);
fwrite(&nb_permutations, sizeof(int), 1, dump);
fclose(dump);
#endif
char* envvar=getenv("ALTREE_PARALLEL");
if (envvar) {
parallel=atoi(envvar);
}
if (parallel == -1) {
parallel=nbproc();
}
if (parallel < 0) {
parallel=0;
}
debug("parallel=%i", parallel);
struct memory *mem=mem_alloc(tree);
compute_chi2s(tree, lcc, &mem->temp, prolonge, results);
int i;
int cases=0;
int controls=0;
for(i=0; i<tree->nb_leaves; i++) {
cases += lcc[i].cases;
controls += lcc[i].controls;
}
if (!parallel) {
for(i=0; i<nb_permutations; i++) {
results += tree->max_depth;
random_clades(tree->nb_leaves, lcc, cases, controls, mem->rand_lcc);
compute_chi2s(tree, mem->rand_lcc, &mem->temp, prolonge, results);
}
} else {
struct paractl ctl = {
.permutation=parallel,
.nb_permutations=nb_permutations,
.tree=tree,
.lcc=lcc,
.cases=cases,
.controls=controls,
.prolonge=prolonge,
.results=results+tree->max_depth,
};
struct parainfo infos[parallel];
pthread_t tids[parallel];
for(i=0; i<parallel; i++) {
infos[i].ctl=&ctl;
infos[i].permutation=i;
pthread_create(&tids[i], NULL, &resampling_worker, &infos[i]);
}
for(i=0; i<parallel; i++) {
pthread_join(tids[i], NULL);
}
}
free_alloc(mem);
return 0;
}
#ifdef MAIN_PROG
int main(int argc, char *argv[])
{
int resampling_chi2(const struct tree *tree, const struct cc *lcc, int prolonge,
int nb_permutations, datatype_t *results, int parallel);
struct tree tree;
FILE* dump=fopen("/tmp/dump.read", "r");
fread(&tree, sizeof(struct tree), 1, dump);
int ld[tree.nb_leaves];
int lp[tree.nb_leaves];
int np[tree.nb_nodes];
struct cc lcc[tree.nb_leaves];
int prolonge;
int nb_permutations;
tree.ld=ld;
tree.lp=lp;
tree.np=np;
fread(tree.ld, sizeof(int), tree.nb_leaves, dump);
fread(tree.lp, sizeof(int), tree.nb_leaves, dump);
fread(tree.np, sizeof(int), tree.nb_nodes, dump);
fread(lcc, sizeof(struct cc), tree.nb_leaves, dump);
fread(&prolonge, sizeof(int), 1, dump);
fread(&nb_permutations, sizeof(int), 1, dump);
nb_permutations=10;
fclose(dump);
datatype_t results[(nb_permutations+1)*tree.max_depth];
//bzero(results, (nb_permutations+1)*tree.max_depth*sizeof(datatype_t));
resampling_chi2(&tree, lcc, prolonge,
nb_permutations, results, 0);
int i,j;
datatype_t *r=results;
for(i=0; i<=nb_permutations; i++) {
for(j=0; j<tree.max_depth; j++) {
printf("\t"CONV, *(r++));
}
printf("\n");
}
/* ensemble_t ens_min_pval; */
/* ens_min_pval=alloc_ensemble(nb_sample); */
/* min=double_permutation(nb_sample, nb_chi2, mat, rep, ens_min_pval); */
/* free_ensemble(ens_min_pval); */
/* for (j=0; j<nb_chi2; j++) { */
/* printf("chi2 niveau %i, pval nc "CONV"\n", j+1, rep[j]); */
/* } */
/* for (i=0; i<nb_sample; i++) { */
/* printf("sample %i, pval min "CONV"\n", i, ens_min_pval[i]); */
/* } */
/* printf("pmin corrigé: "CONV"\n", min); */
/* free_matrice(mat, nb_sample, nb_chi2); */
/* free_replicat(rep); */
return 0;
}
#endif
|