public static void ProcessInEnv(Action <IComputationEnvironment> lambda, IFactory factory) { var env = factory.AllocateComputationEnv(); lambda(env); factory.FreeComputationEnv(env); }
public static IMatrix ProcessInEnv(Func <IComputationEnvironment, IMatrix> lambda, IFactory factory) { var env = factory.AllocateComputationEnv(); var res = lambda(env); factory.FreeComputationEnv(env); return(res); }
public void Permute() { var env = Factory.AllocateComputationEnv(); var values = Enumerable.Range(1, 10).Select(x => (double)x).ToArray(); var v = Factory.GetEncryptedVector(Vector <double> .Build.DenseOfArray(values), EVectorFormat.dense, 1); var I1 = new Tuple <int, double>[] { new Tuple <int, double>(1, 1.0), new Tuple <int, double>(4, 1.0) }; var I2 = new Tuple <int, double>[] { new Tuple <int, double>(3, 1.0), new Tuple <int, double>(6, 1.0) }; var S1 = Vector <double> .Build.SparseOfIndexed(10, I1); var S2 = Vector <double> .Build.SparseOfIndexed(10, I2); var sel1 = Factory.GetPlainVector(S1, EVectorFormat.dense, 1); var sel2 = Factory.GetPlainVector(S2, EVectorFormat.dense, 1); var w = v.Permute(new IVector[] { sel1, sel2 }, new int[] { 1, 2 }, 5, env); var dec = w.Decrypt(env); var exp = Vector <double> .Build.DenseOfArray(new double[] { 2, 4, 0, 5, 7 }); Compare(exp, dec); }
public static void ParallelProcessInEnv(int count, IComputationEnvironment masterEnv, IFactory factory, Action <IComputationEnvironment, int, int> lambda) { if (count < 2) { int taskID = 0; if (masterEnv != null) { for (int k = 0; k < count; k++) { lambda(masterEnv, taskID, k); } } else { ProcessInEnv((env) => { for (int k = 0; k < count; k++) { lambda(env, taskID, k); } }, factory); } } else { int nextItem = -1; int threadCount = (Defaults.ThreadCount > count) ? count : Defaults.ThreadCount; Task[] tasks = new Task[threadCount]; for (int tsk = 0; tsk < tasks.Length; tsk++) { int taskIndex = tsk; tasks[tsk] = Task.Run(() => { int currentTask = (int)Task.CurrentId; var env = factory.AllocateComputationEnv(); int k = 0; while (k < count) { k = Interlocked.Increment(ref nextItem); if (k >= count) { break; } lambda(env, taskIndex, k); } factory.FreeComputationEnv(env); }); } Task.WaitAll(tasks); } }