You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultithreadingTests.cs 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using FluentAssertions;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Tensorflow;
  7. using Tensorflow.Util;
  8. using static Tensorflow.Binding;
  9. namespace TensorFlowNET.UnitTest
  10. {
  11. [TestClass]
  12. public class MultithreadingTests
  13. {
  14. [TestMethod]
  15. public void SessionCreation()
  16. {
  17. ops.uid(); //increment id by one
  18. MultiThreadedUnitTestExecuter.Run(8, Core);
  19. //the core method
  20. void Core(int tid)
  21. {
  22. tf.peak_default_graph().Should().BeNull();
  23. using (var sess = tf.Session())
  24. {
  25. var default_graph = tf.peak_default_graph();
  26. var sess_graph = sess.GetPrivate<Graph>("_graph");
  27. sess_graph.Should().NotBeNull();
  28. default_graph.Should().NotBeNull()
  29. .And.BeEquivalentTo(sess_graph);
  30. }
  31. }
  32. }
  33. [TestMethod]
  34. public void SessionCreation_x2()
  35. {
  36. ops.uid(); //increment id by one
  37. MultiThreadedUnitTestExecuter.Run(16, Core);
  38. //the core method
  39. void Core(int tid)
  40. {
  41. tf.peak_default_graph().Should().BeNull();
  42. //tf.Session created an other graph
  43. using (var sess = tf.Session())
  44. {
  45. var default_graph = tf.peak_default_graph();
  46. var sess_graph = sess.GetPrivate<Graph>("_graph");
  47. sess_graph.Should().NotBeNull();
  48. default_graph.Should().NotBeNull()
  49. .And.BeEquivalentTo(sess_graph);
  50. }
  51. }
  52. }
  53. [TestMethod]
  54. public void GraphCreation()
  55. {
  56. ops.uid(); //increment id by one
  57. MultiThreadedUnitTestExecuter.Run(8, Core);
  58. //the core method
  59. void Core(int tid)
  60. {
  61. tf.peak_default_graph().Should().BeNull();
  62. var beforehand = tf.get_default_graph(); //this should create default automatically.
  63. beforehand.graph_key.Should().NotContain("-0/", "Already created a graph in an other thread.");
  64. tf.peak_default_graph().Should().NotBeNull();
  65. using (var sess = tf.Session())
  66. {
  67. var default_graph = tf.peak_default_graph();
  68. var sess_graph = sess.GetPrivate<Graph>("_graph");
  69. sess_graph.Should().NotBeNull();
  70. default_graph.Should().NotBeNull()
  71. .And.BeEquivalentTo(sess_graph)
  72. .And.BeEquivalentTo(beforehand);
  73. Console.WriteLine($"{tid}-{default_graph.graph_key}");
  74. //var result = sess.run(new object[] {g, a});
  75. //var actualDeriv = result[0].GetData<float>()[0];
  76. //var actual = result[1].GetData<float>()[0];
  77. }
  78. }
  79. }
  80. [TestMethod]
  81. public void Marshal_AllocHGlobal()
  82. {
  83. MultiThreadedUnitTestExecuter.Run(8, Core);
  84. //the core method
  85. void Core(int tid)
  86. {
  87. for (int i = 0; i < 100; i++)
  88. {
  89. Marshal.FreeHGlobal(Marshal.AllocHGlobal(sizeof(int)));
  90. }
  91. }
  92. }
  93. [TestMethod]
  94. public void TensorCreation()
  95. {
  96. //lock (Locks.ProcessWide)
  97. // tf.Session(); //create one to increase next id to 1.
  98. MultiThreadedUnitTestExecuter.Run(8, Core);
  99. //the core method
  100. void Core(int tid)
  101. {
  102. using (var sess = tf.Session())
  103. {
  104. Tensor t = null;
  105. for (int i = 0; i < 100; i++)
  106. {
  107. t = new Tensor(1);
  108. }
  109. }
  110. }
  111. }
  112. [TestMethod]
  113. public void TensorCreation_Array()
  114. {
  115. //lock (Locks.ProcessWide)
  116. // tf.Session(); //create one to increase next id to 1.
  117. MultiThreadedUnitTestExecuter.Run(8, Core);
  118. //the core method
  119. void Core(int tid)
  120. {
  121. //tf.Session created an other graph
  122. using (var sess = tf.Session())
  123. {
  124. Tensor t = null;
  125. for (int i = 0; i < 100; i++)
  126. {
  127. t = new Tensor(new int[] {1, 2, 3});
  128. }
  129. }
  130. }
  131. }
  132. [TestMethod]
  133. public void TensorCreation_Undressed()
  134. {
  135. //lock (Locks.ProcessWide)
  136. // tf.Session(); //create one to increase next id to 1.
  137. MultiThreadedUnitTestExecuter.Run(8, Core);
  138. //the core method
  139. unsafe void Core(int tid)
  140. {
  141. using (var sess = tf.Session())
  142. {
  143. Tensor t = null;
  144. for (int i = 0; i < 100; i++)
  145. {
  146. var v = (int*) Marshal.AllocHGlobal(sizeof(int));
  147. c_api.DeallocatorArgs _deallocatorArgs = new c_api.DeallocatorArgs();
  148. var handle = c_api.TF_NewTensor(typeof(int).as_dtype(), dims: new long[0], num_dims: 0,
  149. data: (IntPtr) v, len: (UIntPtr) sizeof(int),
  150. deallocator: (IntPtr data, IntPtr size, ref c_api.DeallocatorArgs args) => Marshal.FreeHGlobal(data),
  151. ref _deallocatorArgs);
  152. c_api.TF_DeleteTensor(handle);
  153. }
  154. }
  155. }
  156. }
  157. [TestMethod]
  158. public void SessionRun()
  159. {
  160. MultiThreadedUnitTestExecuter.Run(8, Core);
  161. //the core method
  162. void Core(int tid)
  163. {
  164. tf.peak_default_graph().Should().BeNull();
  165. //graph is created automatically to perform create these operations
  166. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  167. var a2 = tf.constant(new[] {3f}, shape: new[] {1});
  168. var math = a1 + a2;
  169. for (int i = 0; i < 100; i++)
  170. {
  171. using (var sess = tf.Session())
  172. {
  173. sess.run(math).GetAtIndex<float>(0).Should().Be(5);
  174. }
  175. }
  176. }
  177. }
  178. [TestMethod]
  179. public void SessionRun_InsideSession()
  180. {
  181. MultiThreadedUnitTestExecuter.Run(8, Core);
  182. //the core method
  183. void Core(int tid)
  184. {
  185. using (var sess = tf.Session())
  186. {
  187. tf.peak_default_graph().Should().NotBeNull();
  188. //graph is created automatically to perform create these operations
  189. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  190. var a2 = tf.constant(new[] {3f}, shape: new[] {1});
  191. var math = a1 + a2;
  192. var result = sess.run(math);
  193. result[0].GetAtIndex<float>(0).Should().Be(5);
  194. }
  195. }
  196. }
  197. [TestMethod]
  198. public void SessionRun_Initialization()
  199. {
  200. MultiThreadedUnitTestExecuter.Run(8, Core);
  201. //the core method
  202. void Core(int tid)
  203. {
  204. using (var sess = tf.Session())
  205. {
  206. tf.peak_default_graph().Should().NotBeNull();
  207. //graph is created automatically to perform create these operations
  208. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  209. var a2 = tf.constant(new[] {3f}, shape: new[] {1});
  210. var math = a1 + a2;
  211. }
  212. }
  213. }
  214. [TestMethod]
  215. public void SessionRun_Initialization_OutsideSession()
  216. {
  217. MultiThreadedUnitTestExecuter.Run(8, Core);
  218. //the core method
  219. void Core(int tid)
  220. {
  221. tf.peak_default_graph().Should().BeNull();
  222. //graph is created automatically to perform create these operations
  223. var a1 = tf.constant(new[] {2f}, shape: new[] {1});
  224. var a2 = tf.constant(new[] {3f}, shape: new[] {1});
  225. var math = a1 + a2;
  226. }
  227. }
  228. }
  229. }