public static List <OthelloGame> ReadAllGames3(string path) {//Parallel ForEach for files & games, all at once, outdated List <OthelloGame> gameRepo = new List <OthelloGame>(); List <string> files = GetFiles(path); object gameTransferLock = new object(); List <Task> gameTransferTask = new List <Task>(); Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, (file) => //For each file, read games { object fileGameLock = new object(); List <OthelloGame> fileGameRepo = new List <OthelloGame>(); var stopwatch = System.Diagnostics.Stopwatch.StartNew(); List <ThorGame> games = FileIO.ReadThorFile(file); //Parallel.ForEach(games, (tgame) => //for each game, transfer to OthelloGame //{ foreach (ThorGame tgame in games) { OthelloGame oGame; try { oGame = new OthelloGame(tgame); lock (fileGameLock) { fileGameRepo = fileGameRepo.Concat(OthelloGame.GetAllGameRotations(oGame)).ToList(); } } catch (Exception) { System.Diagnostics.Debug.WriteLine("failed a THOR->OthelloGame Transformation"); } } //}); lock (gameTransferLock) { gameRepo = gameRepo.Concat(fileGameRepo).ToList(); } if (8 * games.Count != fileGameRepo.Count) { Console.WriteLine("Games have been lost, " + fileGameRepo.Count + " / " + (8 * games.Count) + " games transferred : " + ((double)fileGameRepo.Count / (8 * games.Count)) + " %"); } stopwatch.Stop(); Console.WriteLine("Elapsed time for transferring " + file + " info= {0}", stopwatch.Elapsed); }); return(gameRepo); }
public static List <OthelloGame> ReadAllGames(string path) {//Task per game in a given File, tackles one file at a time List <OthelloGame> gameRepo = new List <OthelloGame>(); List <string> files = GetFiles(path); object gameTransferLock = new object(); foreach (var file in files) //For each file, read games { object fileGameLock = new object(); List <OthelloGame> fileGameRepo = new List <OthelloGame>(); List <Task> fileTransferTask = new List <Task>(); var stopwatch = System.Diagnostics.Stopwatch.StartNew(); List <ThorGame> games = FileIO.ReadThorFile(file); foreach (ThorGame tgame in games) //for each game, transfer to OthelloGame { OthelloGame oGame; fileTransferTask.Add(Task.Run(() => { try { oGame = new OthelloGame(tgame); lock (fileGameLock) { fileGameRepo = fileGameRepo.Concat(OthelloGame.GetAllGameRotations(oGame)).ToList(); } } catch (Exception) { System.Diagnostics.Debug.WriteLine("failed a THOR->OthelloGame Transformation"); } })); } Task.WaitAll(fileTransferTask.ToArray()); foreach (Task t in fileTransferTask) { t.Dispose(); } gameRepo = gameRepo.Concat(fileGameRepo).ToList(); if (8 * games.Count != fileGameRepo.Count) { Console.WriteLine("Games have been lost, " + fileGameRepo.Count + " / " + (8 * games.Count) + " games transferred : " + ((double)fileGameRepo.Count / (8 * games.Count)) + " %"); } stopwatch.Stop(); Console.WriteLine("Elapsed time for transferring " + file + " info= {0}", stopwatch.Elapsed); } return(gameRepo); }
public static List <OthelloGame> ReadAllGames2(string path) {//Thread per File, tackles all at once, outdated List <OthelloGame> gameRepo = new List <OthelloGame>(); List <Thread> fileIOThreadList = new List <Thread>(); List <string> files = GetFiles(path); foreach (var file in files) { fileIOThreadList.Add(new Thread(() => { List <OthelloGame> fileGameRepo = new List <OthelloGame>(); var stopwatch = System.Diagnostics.Stopwatch.StartNew(); List <ThorGame> games = ReadThorFile(file); foreach (ThorGame tgame in games) { OthelloGame oGame; try { oGame = new OthelloGame(tgame); fileGameRepo = fileGameRepo.Concat(OthelloGame.GetAllGameRotations(oGame)).ToList(); } catch (Exception) { System.Diagnostics.Debug.WriteLine("failed a THOR->OthelloGame Transformation"); } } gameRepo = gameRepo.Concat(fileGameRepo).ToList(); stopwatch.Stop(); Console.WriteLine("Elapsed time for transferring " + file + " info= {0}", stopwatch.Elapsed); })); } foreach (Thread t in fileIOThreadList) { t.Start(); } foreach (Thread t in fileIOThreadList) { t.Join(); } return(gameRepo); }