Esempio n. 1
0
        private void ProcessSnapshotWithArrays(short snap)
        {
            //DebugOut.PrintLine("PROCESSING SNAPSHOT WITH ARRAYS" + snap);
            // using this only if needed
            bool writeParticlesBool = true;
            bool writeIndexBool = true;

            List<SnapFileSummary> results = new List<SnapFileSummary>();
            //and make the directory, just to be safe
            try
            {
                Directory.CreateDirectory(outPath);

            }
            catch (IOException e)
            {
                globals.summary.addError(e.Message);
                return;
            }

            int curFile = firstSnapshotFile; // index of current read/write file
            int numFile = 0;

            if (lastSnapshotFile < firstSnapshotFile)
            {
                lastSnapshotFile = firstSnapshotFile;
                numFile = -1;
            }

            while (curFile <= lastSnapshotFile)
            {
                //DebugOut.PrintLine("..file " + curFile + "/" + lastSnapshotFile);
                Console.WriteLine("..file " + curFile + "/" + lastSnapshotFile);
                SnapFileSummary currentFileSummary = new SnapFileSummary();
                results.Add(currentFileSummary);
                currentFileSummary.start = DateTime.Now;
                currentFileSummary.fileNumber = curFile;

                string filename = "";
                try
                {
                    filename = GetSnapFile(inPath, snap, snapshotFilePrefix, curFile);
                    currentFileSummary.inFileName = filename;
                }

                catch (Exception e)
                {
                    currentFileSummary.badStatus = true;
                    currentFileSummary.statusMessage = e.Message;
                    globals.summary.setFileSummaries(results);
                    return;
                }
                // now load the file
                #region mainloop
                try
                {
                    SnapFile curSnap = new SnapFile(filename);
                    // and open the stream for writing
                    using (SqlBinaryWriter particleBinWriter = new SqlBinaryWriter(new FileStream(GetSnapDefault(outPath, snap, snapshotFilePrefix, curFile), FileMode.Create)),
                                           indexBinWriter = new SqlBinaryWriter(new FileStream(GetSnapDefault(outPath, snap, "rev_index_", curFile), FileMode.Create)))
                    {
                        Structs[] parts = new Structs[curSnap.numSample];
                        //record destination path in file summary
                        currentFileSummary.outFileName = GetSnapDefault(outPath, snap, snapshotFilePrefix, curFile);
                        currentFileSummary.indexFileName = GetSnapDefault(outPath, snap, "rev_index_", curFile);
                        // now write each particle into the array
                        for (int i = 0; i < curSnap.numSample; i++)
                        {
                            parts[i].x = curSnap.pos[i, 0];
                            parts[i].y = curSnap.pos[i, 1];
                            parts[i].z = curSnap.pos[i, 2];
                            parts[i].vx = curSnap.vel[i, 0];
                            parts[i].vy = curSnap.vel[i, 1];
                            parts[i].vz = curSnap.vel[i, 2];
                            parts[i].snapnum = snap;
                            parts[i].id = curSnap.id[i];
                            // add in highest-order bit
                            parts[i].id |= ((UInt64)curSnap.nLargeSims[1]) << 32;
                            // make ph-key
                            parts[i].phkey = GetPHKey(parts[i].x, parts[i].y, parts[i].z);

                        }
                        // now sort before writing files
                        // TODO
                        // TBD this may not be necessary if the particles
                        // are sorted in the files already.
                        Array.Sort<Structs>(parts, new ParticleComparator());

                        /*
                         * Sue: This is where you will want to put in a partitioning key
                         * Every time there is a call to particleBinWriter.WriteCell(cell), a single row is written.
                         * The best way to do it would be to just add an argument to the WriteCell method
                         * which is in SqlBinaryWriter.cs on line 113
                         */
                        Cell cell = new Cell(snap);
                        int currentPHkey = -1;
                        LoaderParamSingleton pars = LoaderParamSingleton.getInstance();
                        for (int i = 0; i < curSnap.numSample; i++)
                        {
                            if (parts[i].phkey != currentPHkey)
                            {
                                if (cell.Count > 0)
                                {
                                    // TODO(INDEX) create Cell index here
                                    if (writeParticlesBool)
                                    {
                                        particleBinWriter.WriteCell(cell);
                                    }
                                    //*********************************************************
                                    if (writeIndexBool)
                                    {
                                        List<ReverseIndexEntry> index = cell.createIndex();
                                        indexBinWriter.WriteReverseIndex(index);
                                    }
                                    //*********************************************************

                                    /*
                                    if (pars.firstSnapLoaded)
                                    {
                                        foreach (ReverseIndexEntry entry in index)
                                        {
                                            entry.WriteBinaryFirstSnap(indexBinWriter);
                                        }
                                    }
                                    else
                                    {
                                        foreach (ReverseIndexEntry entry in index)
                                        {
                                            entry.WriteBinaryForMerge(indexBinWriter);
                                        }
                                    }*/
                                }
                                currentPHkey = parts[i].phkey;
                                cell.Init(currentPHkey);
                            }
                            cell.AddToCell(parts[i]);
                        }
                        if (cell.Count > 0)
                        {
                            if (writeParticlesBool)
                            {
                                particleBinWriter.WriteCell(cell);
                            }
                            //*********************************************************
                            if (writeIndexBool)
                            {
                                List<ReverseIndexEntry> index = cell.createIndex();
                                indexBinWriter.WriteReverseIndex(index);
                            }
                            //*********************************************************
                            /*if (pars.firstSnapLoaded)
                            {
                                foreach (ReverseIndexEntry entry in index)
                                {
                                    entry.WriteBinaryFirstSnap(indexBinWriter);
                                }
                            }
                            else
                            {
                                foreach (ReverseIndexEntry entry in index)
                                {
                                    entry.WriteBinaryForMerge(indexBinWriter);
                                }
                            }
                            */
                        }

                        // and add a bulk insert
                        //DebugOut.AddCommand(GetSnapDefault(outPath, snap, snapshotFilePrefix, curFile), snapshotTable);
                        //Console.WriteLine("..wrote " + curSnap.numSample + "/" + curSnap.numTotals[1] + " points");
                        //DebugOut.PrintLine("..wrote " + curSnap.numSample + "/" + curSnap.numTotals[1] + " points");
                    }

                    // and set numFiles
                    if (numFile == -1)
                    {
                        numFile = (int)curSnap.numSubfiles - firstSnapshotFile;
                        lastSnapshotFile = (int)curSnap.numSubfiles - 1;
                    }

                    currentFileSummary.end = DateTime.Now;
                    currentFileSummary.duration = results[curFile].end - results[curFile].start;
                    currentFileSummary.numParticles = curSnap.numSample;

                    if (curSnap.numSample < (int)(0.01 * LoaderParamSingleton.getInstance().particlesPerSnap))
                    {
                        currentFileSummary.warning = true;
                        currentFileSummary.warningMessage = "Less than 1% of particles in snapshot in this file";
                    }
                    if (curSnap.numSample > LoaderParamSingleton.getInstance().particlesPerSnap)
                    {
                        currentFileSummary.warning = true;
                        currentFileSummary.warningMessage = "More particles in file than are supposed to be in snapshot";
                    }

                    if(currentFileSummary.outFileName != null)
                    {
                        globals.summary.AddSnapBCPCommand(currentFileSummary.outFileName);
                        globals.summary.AddIndexBCPCommand(currentFileSummary.indexFileName);
                    }
                    curFile++;
                    // avoid outofmemory errors
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                catch (Exception e)
                {
                    currentFileSummary.badStatus = true;
                    currentFileSummary.statusMessage = e.Message;
                    globals.summary.setFileSummaries(results);
                    return;
                }
                #endregion mainloop

            }
            globals.summary.setFileSummaries(results);
        }
Esempio n. 2
0
        private void ProcessSnapshotWithArrays(short snap)
        {
            //DebugOut.PrintLine("PROCESSING SNAPSHOT WITH ARRAYS" + snap);
            // using this only if needed

            List<SnapFileSummary> results = new List<SnapFileSummary>();
            //and make the directory, just to be safe
            try
            {
                Directory.CreateDirectory(outPath);

            }
            catch (IOException e)
            {
                globals.summary.addError(e.Message);
                return;
            }

            int curFile = firstSnapshotFile; // index of current read/write file
            int numFile = 0;

            if (lastSnapshotFile < firstSnapshotFile)
            {
                lastSnapshotFile = firstSnapshotFile;
                numFile = -1;
            }

            while (curFile <= lastSnapshotFile)
            {
                //DebugOut.PrintLine("..file " + curFile + "/" + lastSnapshotFile);
                Console.WriteLine("..file " + curFile + "/" + lastSnapshotFile);
                SnapFileSummary currentFileSummary = new SnapFileSummary();
                results.Add(currentFileSummary);
                currentFileSummary.start = DateTime.Now;
                currentFileSummary.fileNumber = curFile;

                string filename = "";
                try
                {
                    filename = GetSnapFile(inPath, snap, snapshotFilePrefix, curFile);
                    currentFileSummary.inFileName = filename;
                }

                catch (Exception e)
                {
                    currentFileSummary.badStatus = true;
                    currentFileSummary.statusMessage = e.Message;
                    globals.summary.setFileSummaries(results);
                    return;
                }
                // now load the file
                try
                {
                    SnapFile curSnap = new SnapFile(filename);
                    // and open the stream for writing
                    using (SqlBinaryWriter binwriter = new SqlBinaryWriter(new FileStream(GetSnapDefault(outPath, snap, snapshotFilePrefix, curFile), FileMode.Create)))
                    {
                        Structs[] parts = new Structs[curSnap.numSample];
                        //record destination path in file summary
                        currentFileSummary.outFileName = GetSnapDefault(outPath, snap, snapshotFilePrefix, curFile);
                        // now write each particle into the array
                        for (int i = 0; i < curSnap.numSample; i++)
                        {
                            parts[i].x = curSnap.pos[i, 0];
                            parts[i].y = curSnap.pos[i, 1];
                            parts[i].z = curSnap.pos[i, 2];
                            parts[i].vx = curSnap.vel[i, 0];
                            parts[i].vy = curSnap.vel[i, 1];
                            parts[i].vz = curSnap.vel[i, 2];
                            parts[i].snapnum = snap;
                            parts[i].id = curSnap.id[i];
                            // add in highest-order bit
                            parts[i].id |= ((UInt64)curSnap.nLargeSims[1]) << 32;
                            // make ph-key
                            parts[i].phkey = GetPHKey(parts[i].x, parts[i].y, parts[i].z);

                        }
                        // now sort before writing files
                        // TODO
                        //TBD this may not be necessary if the particles
                        // are sorted in the files already.
                        Array.Sort<Structs>(parts, new ParticleComparator());

                        Cell cell = new Cell(snap);
                        int currentPHkey = -1;
                        for (int i = 0; i < curSnap.numSample; i++)
                        {
                            if (parts[i].phkey != currentPHkey)
                            {
                                if (cell.Count > 0)
                                    binwriter.WriteCell(cell);
                                currentPHkey = parts[i].phkey;
                                cell.Init(currentPHkey);
                            }
                            cell.AddToCell(parts[i]);
                        }
                        if (cell.Count > 0)
                            binwriter.WriteCell(cell);

                        // and add a bulk insert
                        //DebugOut.AddCommand(GetSnapDefault(outPath, snap, snapshotFilePrefix, curFile), snapshotTable);
                        Console.WriteLine("..wrote " + curSnap.numSample + "/" + curSnap.numTotals[1] + " points");
                        DebugOut.PrintLine("..wrote " + curSnap.numSample + "/" + curSnap.numTotals[1] + " points");
                    }

                    // and set numFiles
                    if (numFile == -1)
                    {
                        numFile = (int)curSnap.numSubfiles - firstSnapshotFile;
                        lastSnapshotFile = (int)curSnap.numSubfiles - 1;
                    }

                    currentFileSummary.end = DateTime.Now;
                    currentFileSummary.duration = results[curFile].end - results[curFile].start;
                    currentFileSummary.numParticles = curSnap.numSample;

                    if (curSnap.numSample < (int)(0.01 * LoaderConstants.particlesPerSnap))
                    {
                        currentFileSummary.warning = true;
                        currentFileSummary.warningMessage = "Less than 1% of particles in snapshot in this file";
                    }
                    if (curSnap.numSample > LoaderConstants.particlesPerSnap)
                    {
                        currentFileSummary.warning = true;
                        currentFileSummary.warningMessage = "More particles in file than are supposed to be in snapshot";
                    }

                    if(currentFileSummary.outFileName != null)
                    {
                        globals.summary.AddSnapBCPCommand(currentFileSummary.outFileName);
                    }
                    curFile++;
                    // avoid outofmemory errors
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                catch (Exception e)
                {
                    currentFileSummary.badStatus = true;
                    currentFileSummary.statusMessage = e.Message;
                    globals.summary.setFileSummaries(results);
                    return;
                }

            }
            globals.summary.setFileSummaries(results);
        }