|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- using System;
- using System.Collections.Generic;
- using System.Runtime.InteropServices;
- using FluentAssertions;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Tensorflow;
- using Tensorflow.Util;
- using static Tensorflow.Binding;
-
- namespace TensorFlowNET.UnitTest
- {
- [TestClass]
- public class MultithreadingTests
- {
- [TestMethod]
- public void SessionCreation()
- {
- ops.uid(); //increment id by one
-
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- tf.peak_default_graph().Should().BeNull();
-
- using (var sess = tf.Session())
- {
- var default_graph = tf.peak_default_graph();
- var sess_graph = sess.GetPrivate<Graph>("_graph");
- sess_graph.Should().NotBeNull();
- default_graph.Should().NotBeNull()
- .And.BeEquivalentTo(sess_graph);
- }
- }
- }
-
- [TestMethod]
- public void SessionCreation_x2()
- {
- ops.uid(); //increment id by one
-
- MultiThreadedUnitTestExecuter.Run(16, Core);
-
- //the core method
- void Core(int tid)
- {
- tf.peak_default_graph().Should().BeNull();
- //tf.Session created an other graph
- using (var sess = tf.Session())
- {
- var default_graph = tf.peak_default_graph();
- var sess_graph = sess.GetPrivate<Graph>("_graph");
- sess_graph.Should().NotBeNull();
- default_graph.Should().NotBeNull()
- .And.BeEquivalentTo(sess_graph);
- }
- }
- }
-
- [TestMethod]
- public void GraphCreation()
- {
- ops.uid(); //increment id by one
-
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- tf.peak_default_graph().Should().BeNull();
- var beforehand = tf.get_default_graph(); //this should create default automatically.
- beforehand.graph_key.Should().NotContain("-0/", "Already created a graph in an other thread.");
- tf.peak_default_graph().Should().NotBeNull();
-
- using (var sess = tf.Session())
- {
- var default_graph = tf.peak_default_graph();
- var sess_graph = sess.GetPrivate<Graph>("_graph");
- sess_graph.Should().NotBeNull();
- default_graph.Should().NotBeNull()
- .And.BeEquivalentTo(sess_graph)
- .And.BeEquivalentTo(beforehand);
-
- Console.WriteLine($"{tid}-{default_graph.graph_key}");
-
- //var result = sess.run(new object[] {g, a});
- //var actualDeriv = result[0].GetData<float>()[0];
- //var actual = result[1].GetData<float>()[0];
- }
- }
- }
-
-
- [TestMethod]
- public void Marshal_AllocHGlobal()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- for (int i = 0; i < 100; i++)
- {
- Marshal.FreeHGlobal(Marshal.AllocHGlobal(sizeof(int)));
- }
- }
- }
-
- [TestMethod]
- public void TensorCreation()
- {
- //lock (Locks.ProcessWide)
- // tf.Session(); //create one to increase next id to 1.
-
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- using (var sess = tf.Session())
- {
- Tensor t = null;
- for (int i = 0; i < 100; i++)
- {
- t = new Tensor(1);
- }
- }
- }
- }
-
- [TestMethod]
- public void TensorCreation_Array()
- {
- //lock (Locks.ProcessWide)
- // tf.Session(); //create one to increase next id to 1.
-
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- //tf.Session created an other graph
- using (var sess = tf.Session())
- {
- Tensor t = null;
- for (int i = 0; i < 100; i++)
- {
- t = new Tensor(new int[] {1, 2, 3});
- }
- }
- }
- }
-
- [TestMethod]
- public void TensorCreation_Undressed()
- {
- //lock (Locks.ProcessWide)
- // tf.Session(); //create one to increase next id to 1.
-
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- unsafe void Core(int tid)
- {
- using (var sess = tf.Session())
- {
- Tensor t = null;
- for (int i = 0; i < 100; i++)
- {
- var v = (int*) Marshal.AllocHGlobal(sizeof(int));
- c_api.DeallocatorArgs _deallocatorArgs = new c_api.DeallocatorArgs();
- var handle = c_api.TF_NewTensor(typeof(int).as_dtype(), dims: new long[0], num_dims: 0,
- data: (IntPtr) v, len: (UIntPtr) sizeof(int),
- deallocator: (IntPtr data, IntPtr size, ref c_api.DeallocatorArgs args) => Marshal.FreeHGlobal(data),
- ref _deallocatorArgs);
- c_api.TF_DeleteTensor(handle);
- }
- }
- }
- }
-
- [TestMethod]
- public void SessionRun()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- tf.peak_default_graph().Should().BeNull();
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] {2f}, shape: new[] {1});
- var a2 = tf.constant(new[] {3f}, shape: new[] {1});
- var math = a1 + a2;
- for (int i = 0; i < 100; i++)
- {
- using (var sess = tf.Session())
- {
- sess.run(math).GetAtIndex<float>(0).Should().Be(5);
- }
- }
- }
- }
-
- [TestMethod]
- public void SessionRun_InsideSession()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- using (var sess = tf.Session())
- {
- tf.peak_default_graph().Should().NotBeNull();
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] {2f}, shape: new[] {1});
- var a2 = tf.constant(new[] {3f}, shape: new[] {1});
- var math = a1 + a2;
-
- var result = sess.run(math);
- result[0].GetAtIndex<float>(0).Should().Be(5);
- }
- }
- }
-
- [TestMethod]
- public void SessionRun_Initialization()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- using (var sess = tf.Session())
- {
- tf.peak_default_graph().Should().NotBeNull();
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] {2f}, shape: new[] {1});
- var a2 = tf.constant(new[] {3f}, shape: new[] {1});
- var math = a1 + a2;
- }
- }
- }
-
- [TestMethod]
- public void SessionRun_Initialization_OutsideSession()
- {
- MultiThreadedUnitTestExecuter.Run(8, Core);
-
- //the core method
- void Core(int tid)
- {
- tf.peak_default_graph().Should().BeNull();
- //graph is created automatically to perform create these operations
- var a1 = tf.constant(new[] {2f}, shape: new[] {1});
- var a2 = tf.constant(new[] {3f}, shape: new[] {1});
- var math = a1 + a2;
- }
- }
- }
- }
|