private void WriteFile(string fileName, IEnumerable <string> toWrite = null)
        {
            FileInfo file = new FileInfo(fileName);

            file.Directory?.Create();

            File.WriteAllLines(fileName, toWrite ?? new List <string>());

            CreatedFiles.Add(fileName);
        }
예제 #2
0
        /// <summary>
        /// creates a new instance of the MATLAB connector.
        /// </summary>
        /// <param name="Flav">
        /// octave or MATLAB
        /// </param>
        /// <param name="ExecutablePath">
        /// Where to find the executable on the current system.
        /// If NULL, the standard installation path is assumed.
        /// In the case of Cygwin/octave, the path to the Cygwin bash.exe;
        /// </param>
        /// <param name="WorkingPath">
        /// working directory of the MATLAB instance;
        /// if NULL, a temporary directory is created.
        /// </param>
        public BatchmodeConnector(string WorkingPath = null)
        {
            ilPSP.MPICollectiveWatchDog.Watch(csMPI.Raw._COMM.WORLD);
            csMPI.Raw.Comm_Rank(csMPI.Raw._COMM.WORLD, out Rank);
            this.m_Flav = Flav;

            // create/check working path
            // =========================

            if (Rank == 0)
            {
                if (WorkingPath == null)
                {
                    var  rnd    = new Random();
                    bool Exists = false;
                    do
                    {
                        var tempPath = Path.GetTempPath();
                        var tempDir  = rnd.Next().ToString();
                        WorkingDirectory = new DirectoryInfo(Path.Combine(tempPath, tempDir));
                        Exists           = WorkingDirectory.Exists;
                        if (!Exists)
                        {
                            WorkingDirectory.Create();
                            DelWorkingDir = true;
                        }
                    } while (Exists == true);
                }
                else
                {
                    WorkingDirectory = new DirectoryInfo(WorkingPath);
                    if (!WorkingDirectory.Exists)
                    {
                        throw new ArgumentException("Given working directory is inexistent.");
                    }
                }
            }

            MPIEnviroment.Broadcast(this.WorkingDirectory, 0, csMPI.Raw._COMM.WORLD);

            // more checks
            // ===========
            if (MatlabExecuteable != null)
            {
                if (!File.Exists(MatlabExecuteable))
                {
                    throw new ArgumentException("Unable to find file '" + MatlabExecuteable + "' on this system.");
                }
            }


            // setup MATLAB process
            // ====================

            if (Rank == 0)
            {
                psi = new ProcessStartInfo();
                psi.WorkingDirectory = WorkingDirectory.FullName;
                psi.UseShellExecute  = false;
                //psi.RedirectStandardOutput = true;
                //psi.RedirectStandardError = true;
                //psi.RedirectStandardInput = true;


                PlatformID CurrentSys = System.Environment.OSVersion.Platform;
                switch (CurrentSys)
                {
                case PlatformID.Win32NT:
                case PlatformID.Win32S:
                case PlatformID.Win32Windows: {
                    if (m_Flav == Flavor.Matlab)
                    {
                        if (MatlabExecuteable == null)
                        {
                            MatlabExecuteable = get_program_path("matlab.exe");
                            if (MatlabExecuteable == null)
                            {
                                throw new ApplicationException("Unable to find 'matlab.exe' in your PATH environment; please provide path to 'matlab.exe'.");
                            }
                        }

                        psi.FileName  = MatlabExecuteable;
                        psi.Arguments = "-nosplash -nodesktop -minimize -wait -r " + CMDFILE + " -logfile " + LOGFILE;
                    }
                    else if (m_Flav == Flavor.Octave)
                    {
                        this.Cygwin = true;         // octave and windows must be cygwin!

                        if (MatlabExecuteable == null)
                        {
                            if (File.Exists("c:\\cygwin64\\bin\\octave"))
                            {
                                psi.FileName = "c:\\cygwin64\\bin\\bash.exe";
                            }
                            else if (File.Exists("c:\\cygwin\\bin\\octave"))
                            {
                                psi.FileName = "c:\\cygwin\\bin\\bash.exe";
                            }
                            else
                            {
                                throw new NotSupportedException("Cygwin/Octave are expected to be in the default path: C:\\cygwin or c:\\cygwin64");
                            }
                        }
                        else
                        {
                            //throw new NotSupportedException("Cygwin/Octave are expected to be in the default path: C:\\cygwin or c:\\cygwin64");
                            if (!MatlabExecuteable.EndsWith("bash.exe"))
                            {
                                throw new NotSupportedException("For Cygwin/Octave, the 'MatlabExecuteable' is expected to point to 'bash.exe'.");
                            }

                            psi.FileName = MatlabExecuteable;
                        }
                        psi.Arguments = "--login -c \"cd " + TranslatePath(WorkingDirectory.FullName) + " "
                                        + "&& octave --no-gui " + CMDFILE + ".m" + " > " + LOGFILE + "  \"";
                        //+ "pwd && ls - l && pwd";
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }

                    break;
                }



                case PlatformID.Unix:
                case PlatformID.MacOSX: {
                    throw new NotImplementedException("will implement on request");
                }

                default:
                    throw new NotSupportedException("unable to use MATLAB on " + CurrentSys.ToString());
                }


                CreatedFiles.Add(Path.Combine(WorkingDirectory.FullName, LOGFILE));


                var ScriptsToWrite = new List <Tuple <string, string> >();
                ScriptsToWrite.Add(new Tuple <string, string>("ReadMsr.m", Resource1.ReadMsr));
                ScriptsToWrite.Add(new Tuple <string, string>("SaveVoronoi.m", Resource1.SaveVoronoi));

                foreach (var t in ScriptsToWrite)
                {
                    string name   = t.Item1;
                    string script = t.Item2;
                    var    rmPath = Path.Combine(WorkingDirectory.FullName, name);
                    CreatedFiles.Add(rmPath);
                    File.WriteAllText(rmPath, script);
                }
            }

            // create command file
            // ===================
            if (Rank == 0)
            {
                var p = Path.Combine(WorkingDirectory.FullName, CMDFILE + ".m");
                CommandFile = new StreamWriter(p, false);
                CreatedFiles.Add(p);
            }
        }
        public override async Task Split()
        {
            var  fileInfo     = new FileInfo(FileSplittingInfo.FilePath);
            long originalSize = fileInfo.Length;

            long chunkSize       = (long)Math.Ceiling((double)fileInfo.Length / FileSplittingInfo.NumberOfChunks);
            long totalChunksSize = 0;

            using (var readStream = new FileStream(fileInfo.FullName,
                                                   FileMode.Open,
                                                   FileAccess.Read,
                                                   FileShare.Read,
                                                   BufferSize,
                                                   FileOptions.Asynchronous))
            {
                for (int i = 0; i < FileSplittingInfo.NumberOfChunks; i++)
                {
                    string chunkFileName = ProcessUtils.GetChunkFileName(fileInfo.Name, FileSplittingInfo.DestinationPath, i + 1);
                    using (var writeStream = new FileStream(chunkFileName,
                                                            FileMode.Create,
                                                            FileAccess.Write,
                                                            FileShare.Read,
                                                            BufferSize,
                                                            FileOptions.Asynchronous))
                    {
                        if (!readStream.CanRead)
                        {
                            throw new FileSplitterMergerException($"Can't read file: '{FileSplittingInfo.FilePath}'");
                        }

                        if (!writeStream.CanWrite)
                        {
                            throw new FileSplitterMergerException($"Can't write to path: '{chunkFileName}'");
                        }

                        if (totalChunksSize == originalSize)
                        {
                            // create empty file when all the bytes of the original file have been written (should just be one in edge cases)
                            await writeStream.WriteAsync(new ReadOnlyMemory <byte>());

                            continue;
                        }

                        long currentChunkSize = 0;
                        while (currentChunkSize < chunkSize)
                        {
                            int currentBufferSize = ProcessUtils.GetCurrentBufferSize(currentChunkSize, chunkSize, BufferSize);

                            byte[] currentBuffer = new byte[currentBufferSize];
                            await readStream.ReadAsync(currentBuffer, 0, currentBufferSize);

                            await writeStream.WriteAsync(currentBuffer, 0, currentBufferSize);

                            currentChunkSize += currentBufferSize;

                            if (currentChunkSize == chunkSize)
                            {
                                await writeStream.FlushAsync();

                                await writeStream.DisposeAsync();

                                totalChunksSize += chunkSize;
                                CreatedFiles.Add(chunkFileName);
                            }
                        }
                    }
                    if (originalSize - totalChunksSize > 0 && originalSize - totalChunksSize < chunkSize)
                    {
                        chunkSize = originalSize - totalChunksSize;
                    }
                }
            }

            if (originalSize - totalChunksSize != 0)
            {
                throw new FileSplitterMergerException($"File not split correctly! Difference in bytes: { originalSize - totalChunksSize }");
            }
        }
예제 #4
0
        public override async Task Split()
        {
            var  fileInfo     = new FileInfo(FileSplittingInfo.FilePath);
            long originalSize = fileInfo.Length;

            if (FileSplittingInfo.ChunkSize > originalSize)
            {
                throw new FileSplitterMergerException($"The file size is smaller than the requested chunk size: {originalSize}B");
            }

            if (FileSplittingInfo.ChunkSize == originalSize)
            {
                return;
            }

            int  numberOfChunks        = (int)Math.Ceiling((double)fileInfo.Length / FileSplittingInfo.ChunkSize);
            long totalChunksSize       = 0;
            long currentChunkFinalSize = FileSplittingInfo.ChunkSize;

            using (var readStream = new FileStream(fileInfo.FullName,
                                                   FileMode.Open,
                                                   FileAccess.Read,
                                                   FileShare.Read,
                                                   BufferSize,
                                                   FileOptions.Asynchronous))
            {
                for (int i = 0; i < numberOfChunks; i++)
                {
                    string chunkFileName = ProcessUtils.GetChunkFileName(fileInfo.Name, FileSplittingInfo.DestinationPath, i + 1);

                    using (var writeStream = new FileStream(chunkFileName,
                                                            FileMode.Create,
                                                            FileAccess.Write,
                                                            FileShare.Read,
                                                            BufferSize,
                                                            FileOptions.Asynchronous))
                    {
                        if (!readStream.CanRead)
                        {
                            throw new FileSplitterMergerException($"Can't read file: '{FileSplittingInfo.FilePath}'");
                        }

                        if (!writeStream.CanWrite)
                        {
                            throw new FileSplitterMergerException($"Can't write to path: '{chunkFileName}'");
                        }

                        long currentChunkSize = 0;
                        while (currentChunkSize < currentChunkFinalSize)
                        {
                            int currentBufferSize = ProcessUtils.GetCurrentBufferSize(currentChunkSize, currentChunkFinalSize, BufferSize);

                            byte[] currentBuffer = new byte[currentBufferSize];
                            await readStream.ReadAsync(currentBuffer, 0, currentBufferSize);

                            await writeStream.WriteAsync(currentBuffer, 0, currentBufferSize);

                            currentChunkSize += currentBufferSize;

                            if (currentChunkSize == currentChunkFinalSize)
                            {
                                await writeStream.FlushAsync();

                                await writeStream.DisposeAsync();

                                totalChunksSize += currentChunkFinalSize;
                                CreatedFiles.Add(chunkFileName);
                            }
                        }
                    }
                    if (originalSize - totalChunksSize > 0 && originalSize - totalChunksSize < currentChunkFinalSize)
                    {
                        currentChunkFinalSize = originalSize - totalChunksSize;
                    }
                }
            }

            if (originalSize - totalChunksSize != 0)
            {
                throw new FileSplitterMergerException($"File not split correctly! Difference in bytes: { originalSize - totalChunksSize }");
            }
        }