void LevelParser::parseTileLayer(TiXmlElement* pTileElement, std::vector<Layer*> *pLayers, const std::vector<Tileset>* pTilesets, std::vector<TileLayer*> *m_CollisionsLayer) { TileLayer* pTileLayer = new TileLayer(m_tileSizew, m_tileSizeh, *pTilesets); bool collidable = false; std::vector<std::vector<int>> data; std::string decodedIDs; TiXmlElement* pDataNode; for (TiXmlElement* e = pTileElement->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if (e->Value() == std::string("properties")) { for (TiXmlElement* property = e->FirstChildElement(); property != NULL; property = property->NextSiblingElement()) { if (property->Value() == std::string("property")) { if (property->Attribute("name") == std::string("collidable")) { collidable = true; } } } } if (e->Value() == std::string("data")) { pDataNode = e; } } for (TiXmlNode* e = pDataNode->FirstChild(); e != NULL; e = e->NextSibling()) { TiXmlText* text = e->ToText(); std::string t = text->Value(); decodedIDs = base64_decode(t); } uLongf numGids = m_width * m_height * sizeof(int); std::vector<unsigned> gids(numGids); uncompress((Bytef*)&gids[0], &numGids, (const Bytef*)decodedIDs.c_str(), decodedIDs.size()); std::vector<int> layerRow(m_width); for (int j = 0; j < m_height; j++) { data.push_back(layerRow); } for (int rows = 0; rows < m_height; rows++) { for (int cols = 0; cols < m_width; cols++) { data[rows][cols] = gids[rows * m_width + cols]; } } if (collidable) { m_CollisionsLayer->push_back(pTileLayer); } pTileLayer->setTileIDs(data); pLayers->push_back(pTileLayer); }
void LevelParser::parseTileLayer(TiXmlElement* pTileElement, std::vector<Layer*> *pLayers, const std::vector<Tileset>* pTilesets) { TileLayer* pTileLayer = new TileLayer(m_tileSize, *pTilesets); // tile data std::vector<std::vector<int>> data; std::string decodedIDs; TiXmlElement* pDataNode = NULL; for (TiXmlElement* e = pTileElement->FirstChildElement(); e != NULL; e = e->NextSiblingElement()) { if (e->Value() == std::string("data")) { pDataNode = e; } } for (TiXmlNode* e = pDataNode->FirstChild(); e != NULL; e = e->NextSibling()) { TiXmlText* text = e->ToText(); std::string t = text->Value(); decodedIDs = base64_decode(t); } // uncompress zlib compression uLongf numGids = m_width * m_height * sizeof(int); std::vector<unsigned> gids(numGids); uncompress((Bytef*)&gids[0], &numGids, (const Bytef*)decodedIDs.c_str(), decodedIDs.size()); std::vector<int> layerRow(m_width); for (int j = 0; j < m_height; j++) { data.push_back(layerRow); } for (int rows = 0; rows < m_height; rows++) { for (int cols = 0; cols < m_width; cols++) { data[rows][cols] = gids[rows * m_width + cols]; } } pTileLayer->setTileIDs(data); pLayers->push_back(pTileLayer); }
ErrorCode ReadCCMIO::read_vertices(CCMIOSize_t /* proc */, CCMIOID /* processorID */, CCMIOID verticesID, CCMIOID /* topologyID */, Range *verts, TupleList &vert_map) { CCMIOError error = kCCMIONoErr; // pre-read the number of vertices, so we can pre-allocate & read directly in CCMIOSize_t nverts = CCMIOSIZEC(0); CCMIOEntitySize(&error, verticesID, &nverts, NULL); CHKCCMERR(error, "Couldn't get number of vertices."); // get # dimensions CCMIOSize_t dims; float scale; CCMIOReadVerticesf(&error, verticesID, &dims, NULL, NULL, NULL, CCMIOINDEXC(0), CCMIOINDEXC(1)); CHKCCMERR(error, "Couldn't get number of dimensions."); // allocate vertex space EntityHandle node_handle = 0; std::vector<double*> arrays; readMeshIface->get_node_coords(3, GETINT32(nverts), MB_START_ID, node_handle, arrays); // read vertex coords CCMIOID mapID; std::vector<double> tmp_coords(GETINT32(dims)*GETINT32(nverts)); CCMIOReadVerticesd(&error, verticesID, &dims, &scale, &mapID, &tmp_coords[0], CCMIOINDEXC(0), CCMIOINDEXC(0+nverts)); CHKCCMERR(error, "Trouble reading vertex coordinates."); // copy interleaved coords into moab blocked coordinate space int i = 0, threei = 0; for (; i < nverts; i++) { arrays[0][i] = tmp_coords[threei++]; arrays[1][i] = tmp_coords[threei++]; if (3 == GETINT32(dims)) arrays[2][i] = tmp_coords[threei++]; else arrays[2][i] = 0.0; } // scale, if necessary if (1.0 != scale) { for(i = 0; i < nverts; i++) { arrays[0][i] *= scale; arrays[1][i] *= scale; if (3 == GETINT32(dims)) arrays[2][i] *= scale; } } // read gids for vertices std::vector<int> gids(GETINT32(nverts)); CCMIOReadMap(&error, mapID, &gids[0], CCMIOINDEXC(kCCMIOStart), CCMIOINDEXC(kCCMIOEnd)); CHKCCMERR(error, "Trouble reading vertex global ids."); // put new vertex handles into range, and set gids for them Range new_verts(node_handle, node_handle+nverts-1); ErrorCode rval = mbImpl->tag_set_data(mGlobalIdTag, new_verts, &gids[0]); CHKERR(rval, "Couldn't set gids on vertices."); // pack vert_map with global ids and handles for these vertices #ifdef TUPLE_LIST vert_map.resize(GETINT32(nverts)); for (i = 0; i < GETINT32(nverts); i++) { vert_map.push_back(NULL, &gids[i], &node_handle, NULL); #else for (i = 0; i < GETINT32(nverts); i++) { (vert_map[gids[i]]).push_back(node_handle); #endif node_handle += 1; } if (verts) verts->merge(new_verts); return MB_SUCCESS; } ErrorCode ReadCCMIO::get_processors(CCMIOID stateID, CCMIOID &processorID, CCMIOID &verticesID, CCMIOID &topologyID, CCMIOID &solutionID, std::vector<CCMIOSize_t> &procs, bool & /* has_solution */) { CCMIOSize_t proc = CCMIOSIZEC(0); CCMIOError error = kCCMIONoErr; CCMIONextEntity(&error, stateID, kCCMIOProcessor, &proc, &processorID); CHKCCMERR(error, NULL); if (CCMIOReadProcessor(NULL, processorID, &verticesID, &topologyID, NULL, &solutionID) != kCCMIONoErr) { // Maybe no solution; try again CCMIOReadProcessor(&error, processorID, &verticesID, &topologyID, NULL, NULL); hasSolution = false; } CHKCCMERR(error, NULL); procs.push_back(proc); return MB_SUCCESS; }
// ====================================================================== void GetPtent(const Epetra_RowMatrix& A, Teuchos::ParameterList& List, double* thisns, Teuchos::RCP<Epetra_CrsMatrix>& Ptent, Teuchos::RCP<Epetra_MultiVector>& NextNS, const int domainoffset) { const int nsdim = List.get<int>("null space: dimension",-1); if (nsdim<=0) ML_THROW("null space dimension not given",-1); const Epetra_Map& rowmap = A.RowMatrixRowMap(); const int mylength = rowmap.NumMyElements(); // wrap nullspace into something more handy Epetra_MultiVector ns(View,rowmap,thisns,mylength,nsdim); // vector to hold aggregation information Epetra_IntVector aggs(rowmap,false); // aggregation with global aggregate numbering int naggregates = GetGlobalAggregates(const_cast<Epetra_RowMatrix&>(A),List,thisns,aggs); // build a domain map for Ptent // find first aggregate on proc int firstagg = -1; int offset = -1; for (int i=0; i<mylength; ++i) if (aggs[i]>=0) { offset = firstagg = aggs[i]; break; } offset *= nsdim; if (offset<0) ML_THROW("could not find any aggregate on proc",-2); std::vector<int> coarsegids(naggregates*nsdim); for (int i=0; i<naggregates; ++i) for (int j=0; j<nsdim; ++j) { coarsegids[i*nsdim+j] = offset + domainoffset; ++offset; } Epetra_Map pdomainmap(-1,naggregates*nsdim,&coarsegids[0],0,A.Comm()); // loop aggregates and build ids for dofs std::map<int,std::vector<int> > aggdofs; std::map<int,std::vector<int> >::iterator fool; for (int i=0; i<naggregates; ++i) { std::vector<int> gids(0); aggdofs.insert(std::pair<int,std::vector<int> >(firstagg+i,gids)); } for (int i=0; i<mylength; ++i) { if (aggs[i]<0) continue; std::vector<int>& gids = aggdofs[aggs[i]]; gids.push_back(aggs.Map().GID(i)); } #if 0 // debugging output for (int proc=0; proc<A.Comm().NumProc(); ++proc) { if (proc==A.Comm().MyPID()) { for (fool=aggdofs.begin(); fool!=aggdofs.end(); ++fool) { std::cout << "Proc " << proc << " Aggregate " << fool->first << " Dofs "; std::vector<int>& gids = fool->second; for (int i=0; i<(int)gids.size(); ++i) std::cout << gids[i] << " "; std::cout << std::endl; } } fflush(stdout); A.Comm().Barrier(); } #endif // coarse level nullspace to be filled NextNS = Teuchos::rcp(new Epetra_MultiVector(pdomainmap,nsdim,true)); Epetra_MultiVector& nextns = *NextNS; // matrix Ptent Ptent = Teuchos::rcp(new Epetra_CrsMatrix(Copy,rowmap,nsdim)); // loop aggregates and extract the appropriate slices of the nullspace. // do QR and assemble Q and R to Ptent and NextNS for (fool=aggdofs.begin(); fool!=aggdofs.end(); ++fool) { // extract aggregate-local junk of nullspace const int aggsize = (int)fool->second.size(); Epetra_SerialDenseMatrix Bagg(aggsize,nsdim); for (int i=0; i<aggsize; ++i) for (int j=0; j<nsdim; ++j) Bagg(i,j) = (*ns(j))[ns.Map().LID(fool->second[i])]; // Bagg = Q*R int m = Bagg.M(); int n = Bagg.N(); int lwork = n*10; int info = 0; int k = std::min(m,n); if (k!=n) ML_THROW("Aggregate too small, fatal!",-1); std::vector<double> work(lwork); std::vector<double> tau(k); Epetra_LAPACK lapack; lapack.GEQRF(m,n,Bagg.A(),m,&tau[0],&work[0],lwork,&info); if (info) ML_THROW("Lapack dgeqrf returned nonzero",info); if (work[0]>lwork) { lwork = (int)work[0]; work.resize(lwork); } // get R (stored on Bagg) and assemble it into nextns int agg_cgid = fool->first*nsdim; if (!nextns.Map().MyGID(agg_cgid+domainoffset)) ML_THROW("Missing coarse column id on this proc",-1); for (int i=0; i<n; ++i) for (int j=i; j<n; ++j) (*nextns(j))[nextns.Map().LID(domainoffset+agg_cgid+i)] = Bagg(i,j); // get Q and assemble it into Ptent lapack.ORGQR(m,n,k,Bagg.A(),m,&tau[0],&work[0],lwork,&info); if (info) ML_THROW("Lapack dorgqr returned nonzero",info); for (int i=0; i<aggsize; ++i) { const int actgrow = fool->second[i]; for (int j=0; j<nsdim; ++j) { int actgcol = fool->first*nsdim+j+domainoffset; int errone = Ptent->SumIntoGlobalValues(actgrow,1,&Bagg(i,j),&actgcol); if (errone>0) { int errtwo = Ptent->InsertGlobalValues(actgrow,1,&Bagg(i,j),&actgcol); if (errtwo<0) ML_THROW("Epetra_CrsMatrix::InsertGlobalValues returned negative nonzero",errtwo); } else if (errone) ML_THROW("Epetra_CrsMatrix::SumIntoGlobalValues returned negative nonzero",errone); } } // for (int i=0; i<aggsize; ++i) } // for (fool=aggdofs.begin(); fool!=aggdofs.end(); ++fool) int err = Ptent->FillComplete(pdomainmap,rowmap); if (err) ML_THROW("Epetra_CrsMatrix::FillComplete returned nonzero",err); err = Ptent->OptimizeStorage(); if (err) ML_THROW("Epetra_CrsMatrix::OptimizeStorage returned nonzero",err); return; }
int main(int narg, char **arg) { typedef Tpetra::Map<> map_t; typedef map_t::local_ordinal_type lno_t; typedef map_t::global_ordinal_type gno_t; typedef int scalar_t; // Usual Teuchos MPI stuff Teuchos::GlobalMPISession mpiSession(&narg,&arg); Teuchos::RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); int me = comm->getRank(); // Create a map with duplicated entries (mapWithCopies) // Each rank has 15 IDs, the last five of which overlap with the next rank. lno_t numLocalCoords = 15; lno_t offset = me * 10; Teuchos::Array<gno_t> gids(numLocalCoords); for (lno_t i = 0 ; i < numLocalCoords; i++) gids[i] = static_cast<gno_t> (offset + i); Tpetra::global_size_t numGlobalCoords = Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(); Teuchos::RCP<const map_t> mapWithCopies = rcp(new map_t(numGlobalCoords, gids(), 0, comm)); // Create a new map with IDs uniquely assigned to ranks (oneToOneMap) Teuchos::RCP<const map_t> oneToOneMap = Tpetra::createOneToOne<lno_t, gno_t>(mapWithCopies); // Create vectors with each map typedef Tpetra::Vector<scalar_t, lno_t, gno_t> vector_t; vector_t vecWithCopies(mapWithCopies); vector_t oneToOneVec(oneToOneMap); // Set values in oneToOneVec: each entry == rank for (lno_t i = 0; i < lno_t(oneToOneMap->getNodeNumElements()); i++) oneToOneVec.replaceLocalValue(i, me); // Now import oneToOneVec's values back to vecWithCopies Teuchos::RCP<const Tpetra::Import<lno_t, gno_t> > importer = Tpetra::createImport<lno_t, gno_t>(oneToOneMap, mapWithCopies); vecWithCopies.doImport(oneToOneVec, *importer, Tpetra::REPLACE); // Print the entries of each vector std::cout << me << " ONE TO ONE VEC (" << oneToOneMap->getGlobalNumElements() << "): "; lno_t nlocal = lno_t(oneToOneMap->getNodeNumElements()); for (lno_t i = 0; i < nlocal; i++) std::cout << "[" << oneToOneMap->getGlobalElement(i) << " " << oneToOneVec.getData()[i] << "] "; std::cout << std::endl; // Should see copied vector values when print VEC WITH COPIES std::cout << me << " VEC WITH COPIES (" << mapWithCopies->getGlobalNumElements() << "): "; nlocal = lno_t(mapWithCopies->getNodeNumElements()); for (lno_t i = 0; i < nlocal; i++) std::cout << "[" << mapWithCopies->getGlobalElement(i) << " " << vecWithCopies.getData()[i] << "] "; std::cout << std::endl; return 0; }
void LevelParser::parseTileLayer (TiXmlElement *pTileElement, vector<ILayer *> *pLayers, vector<TileLayer*>* pCollisionLayers,vector<Tileset> *pTilesets){ TileLayer* pTileLayer = new TileLayer(m_tileSize, m_width, m_height, *pTilesets); //Check for layer properties bool collidable(false); for (TiXmlElement* e = pTileElement->FirstChildElement(); e != NULL; e = e->NextSiblingElement()){ //cout << "Checking " << e->Value() << "\n"; if (e->Value() == string("properties")){ for (TiXmlElement* p = e->FirstChildElement(); p != NULL; p = p->NextSiblingElement()){ if (p->Value() == string("property")){ cout << "Now checking " << p->Value() << "\n"; string currentProperty = p->Attribute("name"); if (currentProperty == string("collidable")){ if (p->Attribute("value") == string("true")){ collidable = true; } else { collidable = false; } if (collidable){ cout << "Found collidable layer\n"; } } } } } } //Find data node then store it //pDataNode = findElement("data", pTileElement->FirstChildElement()); bool isBase64 = false ; bool isZlibCompressed = false; TiXmlElement* pDataNode= 0; for (TiXmlElement* e = pTileElement->FirstChildElement(); e != NULL; e = e->NextSiblingElement()){ if (e->Value() == string("data")){ pDataNode = e; //Check if encoded/compressed if (e->Attribute("encoding")){ if (e->Attribute("encoding") == string("base64")){ isBase64 = true; } } if (e->Attribute("compression")){ if (e->Attribute("compression") == string("zlib")){ isZlibCompressed = true; } } } } //Decode data and store string decodedIDs; if (pDataNode && isBase64){ for (TiXmlNode* e = pDataNode->FirstChild(); e != NULL; e = e->NextSibling()){ TiXmlText* text = e ->ToText(); string t = text->Value(); decodedIDs = base64_decode(t); } } //Placeholder for data vector<vector<int>> data; //Calculate number of GIDS present uLongf numGids = m_width * m_height * sizeof(int); vector<unsigned> gids(numGids); //Horizontal register for vector vector<int> layerRow(m_width); //Build empty data vector to fill for(int j = 0 ; j < m_height; j++){ data.push_back(layerRow); } //Compressed data assignment if (isZlibCompressed){ uncompress ((Bytef*)&gids[0], &numGids, (const Bytef*)decodedIDs.c_str(), decodedIDs.size()); for (int rows = 0 ; rows <m_height; rows++){ for (int cols = 0; cols < m_width; cols++){ data[rows][cols] = gids[rows * m_width + cols]; } } } else { //Uncompressed data assignment int index = 0; int tileID = 0; //Find all tiles, assign GID to proper data vector place for (TiXmlElement* e = pDataNode->FirstChildElement(); e != NULL; e = e->NextSiblingElement()){ e->Attribute("gid",&tileID); data[index / m_width][index % m_width] = tileID; index++; } } //Set Tile Layer properties pTileLayer->setTileIDs(data); pTileLayer->setNumColumns(m_width); pTileLayer->setNumRows(m_height); //Save new tile layer to Level cout << "Added new layer\n"; pLayers->push_back(pTileLayer); //Add collision tiles to collision layer if (collidable){ pCollisionLayers->push_back(pTileLayer); cout << "Added new collision layer\n"; } }