@@ -200,16 +200,17 @@ void ModelData::condense(const array<bool>& mask, size_t mask_true) /* remove
200200 foodweb = interaction;
201201}
202202
203- void PanmicticModel::condense ()
203+ unsigned PanmicticModel::condense ()
204204{
205205 auto mask=density != 0 ;
206206 size_t mask_true=sum (mask);
207- if (mask.size ()==mask_true) return ; /* no change ! */
207+ if (mask.size ()==mask_true) return 0 ; /* no change ! */
208208 ModelData::condense (mask,mask_true);
209209 EcolabPoint::condense (mask, mask_true);
210+ return mask.size ()-mask_true;
210211}
211212
212- void SpatialModel::condense ()
213+ unsigned SpatialModel::condense ()
213214{
214215 array<int > total_density (species.size ());
215216 for (auto & i: *this ) total_density+=i->as <EcolabCell>()->density ; // TODO
@@ -221,9 +222,10 @@ void SpatialModel::condense()
221222 auto mask=total_density != 0 ;
222223
223224 size_t mask_true=sum (mask);
224- if (mask.size ()==mask_true) return ; /* no change ! */
225+ if (mask.size ()==mask_true) return 0 ; /* no change ! */
225226 ModelData::condense (mask,mask_true);
226227 for (auto & i: *this ) i->as <EcolabCell>()->condense (mask, mask_true);
228+ return mask.size ()-mask_true;
227229}
228230
229231
@@ -633,59 +635,71 @@ void SpatialModel::generate(unsigned niter)
633635
634636void SpatialModel::migrate ()
635637{
636- // /* each cell gets a distinct random salt value */
637- // forAll ([=,this](EcolabCell& c) {c.salt=c.rand();});
638- //
639- // prepareNeighbours();
640- //
641- // // #ifdef SYCL_LANGUAGE_VERSION
642- // // using ArrayAlloc=graphcode::Allocator<int>;
643- // // using Array=vector<int,ArrayAlloc>;
644- // // using DeltaAlloc=graphcode::Allocator<Array>;
645- // // Array init(species.size(),0,ArrayAlloc(syclQ(),sycl::usm::alloc::device));
646- // // vector<Array,DeltaAlloc> delta(size(), init, DeltaAlloc(syclQ(),sycl::usm::alloc::device));
647- // // #else
648- // // vector<array<int>> delta(size(), array<int>(species.size(),0));
649- // // #endif
650- //
638+ /* each cell gets a distinct random salt value */
639+ hostForAll ([=,this ](EcolabCell& c) {c.salt =c.rand ();});
640+
641+ prepareNeighbours ();
642+
643+ // #ifdef SYCL_LANGUAGE_VERSION
644+ // using ArrayAlloc=graphcode::Allocator<int>;
645+ // using Array=vector<int,ArrayAlloc>;
646+ // using DeltaAlloc=graphcode::Allocator<Array>;
647+ // Array init(species.size(),0,ArrayAlloc(syclQ(),sycl::usm::alloc::device));
648+ // vector<Array,DeltaAlloc> delta(size(), init, DeltaAlloc(syclQ(),sycl::usm::alloc::device));
649+ // #else
650+ vector<array<int >> delta (size (), array<int >(species.size (),0 ));
651+ // #endif
652+
651653// for (auto& c: *this)
652654// c->as<EcolabCell>()->delta.resize(species.size());
653- //
654- // groupedForAll([=,this](EcolabCell& c) {
655- // /* loop over neighbours */
656- // for (auto& n: c)
657- // {
658- // auto& nbr=*n->as<EcolabCell>();
659- // Float salt=c.id<nbr.id? c.salt: nbr.salt;
660- // array_ns::groupAsg(species.size(), [&](size_t i) {
661- // Float m=(tstep-last_mig_tstep) * migration[i] * (nbr.density[i] - c.density[i]);
662- // c.delta[i]+=m + (m!=0.0)*(2*(m>0.0)-1) * salt;
663- // });
664- // }
665- // });
666- // last_mig_tstep=tstep;
667- // syncThreads();
668- // groupedForAll([=,this](EcolabCell& c) {
669- // c.density+=c.delta;
670- // });
655+
656+ hostForAll ([&,this ](EcolabCell& c) {
657+ /* loop over neighbours */
658+ for (auto & n: c)
659+ {
660+ auto & nbr=*n->as <EcolabCell>();
661+ Float salt=c.id <nbr.id ? c.salt : nbr.salt ;
662+ array<Float> m=(tstep-last_mig_tstep) * migration * (nbr.density - c.density );
663+ delta[c.idx ()]+=m + array<Float>((m!=0.0 )*(2 *(m>0.0 )-1 )) * salt;
664+ }
665+ });
666+
667+ array<int > ssum (species.size (),0 );
668+ hostForAll ([&,this ](EcolabCell& c) {
669+ c.density +=delta[c.idx ()];
670+ #if !defined(NDEBUG)
671+ #pragma omp critical
672+ ssum+=delta[c.idx ()];
673+ #endif
674+ });
675+ last_mig_tstep=tstep;
671676
672677 /* assertion testing that population numbers are conserved */
673678#if !defined(NDEBUG) && !defined(SYCL_LANGUAGE_VERSION)
674- array<int > ssum (species.size ()), s (species.size ());
675- unsigned mig=0 , i;
676- for (ssum=0 , i=0 ; i<size (); i++)
677- {
678- ssum+=delta[i];
679- for (size_t j=0 ; j<delta[i].size (); j++)
680- mig+=abs (delta[i][j]);
681- }
682679#ifdef MPI_SUPPORT
680+ array<int > s (species.size ());
683681 MPI_Reduce (ssum.data (),s.data (),s.size (),MPI_INT,MPI_SUM,0 ,MPI_COMM_WORLD);
684682 ssum=s;
685- int m;
686- MPI_Reduce (&mig,&m,1 ,MPI_INT,MPI_SUM,0 ,MPI_COMM_WORLD);
687- mig=m;
688683#endif
684+ if (sum (ssum==0 )!=int (ssum.size ()))
685+ {
686+ for (size_t i=0 ; i<ssum.size (); ++i)
687+ if (ssum[i])
688+ {
689+ cout<<" species " <<i<<" :" <<endl;
690+ for (size_t idx=0 ; idx<size (); ++idx)
691+ if (delta[idx][i])
692+ {
693+ auto & c=*(*this )[idx]->as <EcolabCell>();
694+ cout<<" delta " <<c.id <<" =" <<delta[idx][i]<<" n=" <<c.density [i]<<endl;
695+ for (auto & n: c)
696+ {
697+ auto & nbr=*n->as <EcolabCell>();
698+ cout<<" nbr:" <<nbr.id <<" =" <<nbr.density [i]<<endl;
699+ }
700+ }
701+ }
702+ }
689703 if (myid ()==0 ) assert (sum (ssum==0 )==int (ssum.size ()));
690704#endif
691705}
0 commit comments