00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdio.h>
00034 #include <sys/types.h>
00035 #include "harp3.h"
00036 #include "harp3defects.h"
00037 #ifdef INTERNAL_OUT
00038 # undef INTERNAL_OUT
00039 # define INTERNAL_OUT 1
00040 #else
00041 # define INTERNAL_OUT 0
00042 #endif
00043
00044 #ifndef NBOARDS
00045 # define NBOARDS 1
00046 #endif
00047
00048 #define NWAIT 2+4
00049
00050
00051
00052
00053
00054
00055
00056
00057 unsigned int * base;
00058
00059 unsigned int h3wait_();
00060 static int nboards;
00061
00062 static int ndefects;
00063
00064 void jpdma_index_initialize();
00065
00066 int kill_dum;
00067 void kill_time(i)
00068 int i;
00069 {
00070 int j;
00071 for(j=0;j<i*3;j++)kill_dum = rand();
00072 }
00073
00074 static int njold = 0;
00075 static int njset;
00076
00077 void h3open_()
00078 {
00079 unsigned int * open_harp3();
00080 base =open_harp3();
00081 nboards = get_nboards();
00082 ndefects = get_number_of_defect_chips();
00083 jpdma_index_initialize();
00084 harp3_testrun_();
00085 set_mode(base,255,15);
00086 h3wait_();
00087
00088 njold = 0;
00089
00090 }
00091 void h3open_notest_()
00092 {
00093 unsigned int * open_harp3();
00094 base =open_harp3();
00095 nboards = get_nboards();
00096 ndefects = get_number_of_defect_chips();
00097 jpdma_index_initialize();
00098 set_mode(base,255,15);
00099 h3wait_();
00100 }
00101
00102 void h3close_()
00103 {
00104 close_harp3();
00105 }
00106
00107 int h3npipe_()
00108 {
00109 return get_number_of_pipelines(base);
00110 }
00111
00112 void h3setnboards_(nb)
00113 int * nb;
00114 {
00115 nboards = *nb;
00116 }
00117
00118 int h3getnboards_()
00119 {
00120 return nboards;
00121 }
00122
00123 unsigned int h3wait_()
00124 {
00125 return wait_harp3_until_idle(base);
00126 }
00127
00128 void h3setti_(ti)
00129 double * ti;
00130 {
00131 raw_set_ti(base, ti);
00132 raw_set_ti(base, ti);
00133 }
00134 void h3randomwrite_(addr, dat)
00135 int * addr;
00136 int *dat;
00137 {
00138 dma_random_write(base, *addr, *dat);
00139 }
00140
00141 h3setmode_(mode, iboard)
00142 int *mode;
00143 int *iboard;
00144 {
00145 set_mode(base, *mode, *iboard);
00146 }
00147
00148 h3setled_(mode, iboard)
00149 int *mode;
00150 int *iboard;
00151 {
00152 set_led(base, *mode, *iboard);
00153 }
00154
00155 int h3jpmax_()
00156 {
00157 int jpdma_max_particles();
00158 return jpdma_max_particles();
00159 }
00160
00161
00162
00163 h3jpdma_(nj,xj,vj,aj,jj,mj,tj,pj)
00164 int * nj;
00165 double xj[][3];
00166 double vj[][3];
00167 double aj[][3];
00168 double jj[][3];
00169 double mj[];
00170 double tj[];
00171 int pj[];
00172 {
00173 #define JPMAX 100
00174 int i, k;
00175 int index[JPMAX];
00176 register int p;
00177 if(*nj > JPMAX){
00178 fprintf(stderr,"(h3jpdma) Too large nj (%d > %d\n",nj, JPMAX);
00179 exit(-1);
00180 }
00181 for (i=0; i< *nj; i++){
00182 #if 0
00183 index[i]= pj[i]*3+0x2000000;
00184 #endif
00185 p = pj[i];
00186 index[i]=0x2000000*(p%nboards+1) + 3*(p/nboards);
00187 #if INTERNAL_OUT
00188 printf("index, i, mj = 0x%x %d %g\n",index[i], i, mj[i]);
00189 #endif
00190 }
00191 jpdma_for_array(base, *nj, index,xj,vj,aj,jj,tj,mj);
00192 }
00193
00194 static int jpdma_index_array[2000000];
00195 static int jpdma_index_initialized = 0;
00196 void jpdma_index_initialize()
00197 {
00198 int i;
00199 if(jpdma_index_initialized) return;
00200 jpdma_index_initialized =1;
00201 for (i = 0;i < 2000000; i++){
00202 jpdma_index_array[i+1] =
00203 0x2000000*(i%nboards+1) + 3*(i/nboards);
00204 }
00205 }
00206 h3jpdma_indirect_(nj,hostindex,xj,vj,aj,jj,mj,tj,mode)
00207 int * nj;
00208 int hostindex[];
00209 double xj[][3];
00210 double vj[][3];
00211 double aj[][3];
00212 double jj[][3];
00213 double mj[];
00214 double tj[];
00215 int *mode;
00216 {
00217 #define JPMAX 100
00218 int i, k;
00219 int index[JPMAX];
00220 register int p;
00221 if(*nj > JPMAX){
00222 fprintf(stderr,"(h3jpdma) Too large nj (%d > %d\n",nj, JPMAX);
00223 exit(-1);
00224 }
00225 for (i=0; i< *nj; i++){
00226 #if 0
00227 index[i]= hostindex[i]*3+0x2000000-3;
00228 p = hostindex[i] - 1;
00229 index[i]=0x2000000*(p%nboards+1) + 3*(p/nboards);
00230 #endif
00231 index[i]=jpdma_index_array[hostindex[i]];
00232 #if INTERNAL_OUT
00233 printf("j, mj = 0x%x %d %g\n", index[i], i, mj[i]);
00234 #endif
00235 }
00236 jpdma_for_array_indirect(base, *nj, hostindex,index,xj,vj,aj,jj,tj,mj,*mode);
00237 }
00238
00239
00240 static int buff_used = 0;
00241 h3mjpdma_indirect_(nj,hostindex,xj,vj,aj,jj,mj,tj,mode,buff_id)
00242 int * nj;
00243 int hostindex[];
00244 double xj[][3];
00245 double vj[][3];
00246 double aj[][3];
00247 double jj[][3];
00248 double mj[];
00249 double tj[];
00250 int *mode;
00251 int * buff_id;
00252 {
00253 #define JPMAX 100
00254 register int i;
00255 static int index[JPMAX];
00256 int bufno;
00257 if(*nj > JPMAX){
00258 fprintf(stderr,"(h3jpdma) Too large nj (%d > %d\n",nj, JPMAX);
00259 exit(-1);
00260 }
00261 for (i=0; i< *nj; i+=4){
00262 index[i]=jpdma_index_array[hostindex[i]];
00263 index[i+1]=jpdma_index_array[hostindex[i+1]];
00264 index[i+2]=jpdma_index_array[hostindex[i+2]];
00265 index[i+3]=jpdma_index_array[hostindex[i+3]];
00266 #if INTERNAL_OUT
00267 printf("j, mj = 0x%x %d %g\n", index[i], i, mj[i]);
00268 #endif
00269 }
00270 if(*buff_id >= 0){
00271 bufno = *buff_id;
00272 }else{
00273 bufno = buff_used;
00274 buff_used ++;
00275 }
00276 mjpdma_for_array_indirect(base, *nj, hostindex,index,xj,vj,aj,jj,tj,mj,
00277 *mode, bufno);
00278 }
00279
00280 h3mjpdma_start_(buff_id)
00281 int * buff_id;
00282 {
00283 jpdma_start(base, *buff_id);
00284 }
00285
00286 h3jpdma_flush_()
00287 {
00288 int i;
00289 for(i=0;i<buff_used; i++){
00290 jpdma_start(base, i);
00291 }
00292 buff_used = 0;
00293 }
00294
00295 h3jpdma_reset_()
00296 {
00297 buff_used = 0;
00298 }
00299
00300
00301 h3jpdma_xvtm_(nj,xj,vj,mj,tj,pj)
00302 int * nj;
00303 double xj[][3];
00304 double vj[][3];
00305 double mj[];
00306 double tj[];
00307 int pj[];
00308 {
00309 #define JPMAX 100
00310 int i, k;
00311 int index[JPMAX];
00312 register int p;
00313 if(*nj > JPMAX){
00314 fprintf(stderr,"(h3jpdma) Too large nj (%d > %d\n",nj, JPMAX);
00315 exit(-1);
00316 }
00317 for (i=0; i< *nj; i++){
00318
00319 p = pj[i] - 1;
00320 index[i]=0x2000000*(p%nboards+1) + 3*(p/nboards);
00321 #if INTERNAL_OUT
00322 printf("j, mj = 0x%x %d %g\n", index[i], i, mj[i]);
00323 #endif
00324 }
00325 jpdma_for_array_xvtm(base, *nj, index,xj,vj,tj,mj);
00326 }
00327
00328
00329
00330 void clear_sprious_particles(nj, nb)
00331 int nj;
00332 int nb;
00333 {
00334 static double x[3];
00335 static double v[3];
00336 static double a[3];
00337 static double j[3];
00338 static double m;
00339 static double t;
00340 int j1, jw, k;
00341 for(k=0;k<3;k++){
00342 x[k] =1e50;
00343 v[k] =0.0;
00344 a[k] =0.0;
00345 j[k] =0.0;
00346 }
00347 m = 0.0;
00348 t = 0.0;
00349 if( nj % nb){
00350 j1 = nb-(nj%nb);
00351 for(jw = 0; jw < j1; jw ++){
00352 int jtmp = 0x2000000*(nb-jw) + 3*(nj/nb);
00353
00354 h3wait_();
00355 jpdma_for_array(base, 1, &jtmp,x, v, a, j, &t, &m);
00356 }
00357 }
00358 }
00359
00360
00361
00362 h3calc_(nj,ni,xi,vi,eps2,h2,acc,jerk,pot)
00363 int * nj;
00364 int * ni;
00365 double xi[][3];
00366 double vi[][3];
00367 double eps2[];
00368 double h2[];
00369 double acc[][3];
00370 double jerk[][3];
00371 double pot[];
00372 {
00373 #define IPMAX 100
00374 static ipdma_packet i_particles[IPMAX];
00375 static result_packet real_result[IPMAX];
00376 ipdma_packet * ip;
00377 int i, k;
00378 if(*nj != njold){
00379 clear_sprious_particles(*nj, nboards);
00380 njold = *nj;
00381 njset = ((*nj+nboards - 1)/nboards)*3;
00382 }
00383
00384 if(*ni > IPMAX){
00385 fprintf(stderr,"(h3ipdma) Too large ni (%d > %d\n",ni, IPMAX);
00386 exit(-1);
00387 }
00388
00389 ipdma_for_array_with_real_ni(base, *ni, *ni,xi,vi,eps2,h2);
00390 wait_harp3_until_idle(base);
00391 dma_result_initialize(base);
00392 #ifndef ONEBUS
00393 #ifndef TWOBUS
00394 #ifndef THREEBUS
00395 #ifndef FOURBUS
00396 #ifndef SIXBUS
00397 Present code works only for ONEBUS case....
00398 aho-----------------
00399 #endif
00400 #endif
00401 #endif
00402 #endif
00403 #endif
00404 #ifdef ONEBUS
00405 calc(base, njset, nboards, *ni - 1);
00406 #endif
00407 #ifdef TWOBUS
00408 calc(base, njset, nboards, (*ni)/2 - 1);
00409 #endif
00410 #ifdef THREEBUS
00411 calc(base, njset, nboards, (*ni + ndefects)/3 - 1);
00412 #endif
00413 #ifdef FOURBUS
00414 calc(base, njset, nboards, (*ni + ndefects)/3 - 1);
00415 #endif
00416 {
00417 int i;
00418 int k;
00419 kill_time(1);
00420
00421 }
00422 wait_harp3_until_idle(base);
00423 wait_harp3_until_idle(base);
00424 for(i = 0; i<nboards*NWAIT; i++){
00425 wait_harp3_until_idle(base);
00426 wait_harp3_until_idle(base);
00427 }
00428 {
00429 int i;
00430 i = 1;
00431 kill_time(i);
00432 }
00433 dma_result(base);
00434 #if 0
00435 get_result(base, *ni, real_result);
00436 for(i=0;i< *ni;i++){
00437 for(k=0;k<3;k++)acc[i][k] = real_result[i].acc[k];
00438 for(k=0;k<3;k++)jerk[i][k] = real_result[i].jerk[k];
00439 pot[i] = real_result[i].phi;
00440 }
00441 #else
00442 get_result_for_array(base, *ni, acc,jerk,pot);
00443 #endif
00444
00445 }
00446
00447 h3calc_firsthalf_(nj,ni,xi,vi,eps2,h2)
00448 int * nj;
00449 int * ni;
00450 double xi[][3];
00451 double vi[][3];
00452 double eps2[];
00453 double h2[];
00454 {
00455 #define IPMAX 100
00456 static ipdma_packet i_particles[IPMAX];
00457 ipdma_packet * ip;
00458 int i, k;
00459 if(*nj != njold){
00460 clear_sprious_particles(*nj, nboards);
00461 njold = *nj;
00462 njset = ((*nj+nboards - 1)/nboards)*3;
00463 }
00464 if(*ni > IPMAX){
00465 fprintf(stderr,"(h3ipdma) Too large ni (%d > %d\n",ni, IPMAX);
00466 exit(-1);
00467 }
00468 ipdma_for_array(base, *ni, xi,vi,eps2,h2);
00469 wait_harp3_until_idle(base);
00470 dma_result_initialize(base);
00471
00472 #ifndef ONEBUS
00473 #ifndef TWOBUS
00474 #ifndef THREEBUS
00475 #ifndef FOURBUS
00476 Present code works only for ONEBUS case....
00477 aho-----------------
00478 #endif
00479 #endif
00480 #endif
00481 #endif
00482 #ifdef ONEBUS
00483 calc(base, njset, nboards, *ni - 1);
00484 #endif
00485 #ifdef TWOBUS
00486 calc(base, njset, nboards, (*ni)/2 - 1);
00487 #endif
00488 #ifdef THREEBUS
00489 calc(base, njset, nboards, (*ni + ndefects)/3 - 1);
00490 #endif
00491 #ifdef FOURBUS
00492 calc(base, njset, nboards, (*ni + ndefects)/3 - 1);
00493 #endif
00494 }
00495
00496 h3calc_lasthalf_(ni,acc,jerk,pot)
00497 int * ni;
00498 double acc[][3];
00499 double jerk[][3];
00500 double pot[];
00501 {
00502 static result_packet real_result[IPMAX];
00503 ipdma_packet * ip;
00504 int i, k;
00505 if(*ni > IPMAX){
00506 fprintf(stderr,"(h3ipdma) Too large ni (%d > %d\n",ni, IPMAX);
00507 exit(-1);
00508 }
00509 for(i = 0; i<nboards*NWAIT; i++){
00510 wait_harp3_until_idle(base);
00511 wait_harp3_until_idle(base);
00512 }
00513 dma_result(base);
00514 for(i = 0; i<NWAIT; i++){
00515 wait_harp3_until_idle(base);
00516 wait_harp3_until_idle(base);
00517 }
00518 get_result_for_array(base, *ni, acc,jerk,pot);
00519 }
00520
00521 #define REAL double
00522 void accel_by_harp3_separate_(ni,xi,nj,xj,m, a, pot,eps2)
00523 int *ni;
00524 REAL xi[][3];
00525 int *nj;
00526 REAL xj[][3];
00527 REAL m[];
00528 REAL a[][3];
00529 REAL pot[];
00530 REAL *eps2;
00531 {
00532 h3open_();
00533 calculate_accel_by_harp3_separate_trial_noopen(*ni,xi,*nj,xj,m,
00534 a, pot,*eps2,0);
00535 h3close_();
00536 }
00537
00538
00539 void h3nbread_(nboards)
00540 int * nboards;
00541 {
00542 int get_nb_retval, get_nb_mode;
00543 int iwait;
00544 iwait = 1;
00545 #if INTERNAL_OUT
00546 printf("(h3nbread) nboards = %d\n", *nboards);
00547 #endif
00548 get_nb_retval = get_nb_mode = 1;
00549
00550 while(get_nb_retval){
00551 nb_read_low(base, 250, *nboards);
00552
00553 h3wait_();
00554 h3wait_();
00555 h3wait_();
00556 h3wait_();
00557 h3wait_();
00558 h3wait_();
00559 h3wait_();
00560 h3wait_();
00561
00562 h3wait_();
00563 h3wait_();
00564 h3wait_();
00565
00566 get_nb_retval = get_nb_low(base,get_nb_mode);
00567
00568 h3wait_();
00569 h3wait_();
00570 h3wait_();
00571 h3wait_();
00572 h3wait_();
00573 get_nb_mode =0;
00574 }
00575 }
00576
00577 int compare(i,j)
00578 int *i;
00579 int *j;
00580 {
00581 return *i - *j;
00582 }
00583
00584 int h3nblist_(board, chip, nblist)
00585 int * board;
00586 int * chip;
00587 int * nblist;
00588 {
00589 int construct_nblist(), nnb, compare(), i;
00590 nnb = construct_nblist_low(*board, *chip,1, nblist);
00591 #if INTERNAL_OUT
00592 printf("h3nblist: nnb = %d\n", nnb);
00593 for(i=0;i<nnb;i++){
00594 printf("h3nblist: nb[%d]= %d\n", i, nblist[i]);
00595 }
00596 printf("sort...\n");
00597 #endif
00598 qsort(nblist, nnb, sizeof(int), compare);
00599 #if INTERNAL_OUT
00600 printf("h3nblist: nnb = %d\n", nnb);
00601 for(i=0;i<nnb;i++){
00602 printf("h3nblist: nb[%d]= %d\n", i, nblist[i]);
00603 }
00604 #endif
00605 while ((nnb > 0) && (nblist[nnb-1] >= njold)) nnb--;
00606 for(i=0;i<nnb-1;i++){
00607
00608 while((nblist[i] == nblist[i+1]) && (i<nnb-1)){
00609 int j;
00610 #if INTERNAL_OUT
00611 printf("correcting nblist, %d %d %d %d\n",
00612 i, nnb, nblist[i], nblist[i+1]);
00613 #endif
00614 for(j=i+1; j<nnb-1;j++){
00615 nblist[j] = nblist[j+1];
00616 }
00617 nnb--;
00618 }
00619 }
00620 #if INTERNAL_OUT
00621 printf("h3nblist: adjusted nnb = %d\n", nnb);
00622 #endif
00623 return nnb;
00624 }
00625
00626
00627 accel_by_harp3_separate_noopen_(ni,xi,nj,xj,m, a, pot,eps2)
00628 int *ni;
00629 double xi[][3];
00630 int *nj;
00631 double xj[][3];
00632 double m[];
00633 double a[][3];
00634 double pot[];
00635 double *eps2;
00636 {
00637 calculate_accel_by_harp3_separate_trial_noopen(*ni,xi,*nj,xj,m, a, pot,*eps2,0);
00638 }
00639
00640 int dummy_wait_on_host(idummy)
00641 int * idummy;
00642 {
00643 (*idummy) ++;
00644 return *idummy;
00645 }
00646
00647 int dummy_wait_on_host2(idummy)
00648 int * idummy;
00649 {
00650 (*idummy) ++;
00651 return *idummy;
00652 }
00653