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 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 static Tensorflow.Binding;
  8. namespace TensorFlowNET.UnitTest
  9. {
  10. [TestClass]
  11. public class MultithreadingTests
  12. {
  13. [TestMethod]
  14. public void SessionCreation()
  15. {
  16. tf.Session(); //create one to increase next id to 1.
  17. MultiThreadedUnitTestExecuter.Run(8, Core);
  18. //the core method
  19. void Core(int tid)
  20. {
  21. tf.peak_default_graph().Should().BeNull();
  22. //tf.Session created an other graph
  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 GraphCreation()
  35. {
  36. tf.Graph(); //create one to increase next id to 1.
  37. MultiThreadedUnitTestExecuter.Run(8, Core);
  38. //the core method
  39. void Core(int tid)
  40. {
  41. tf.peak_default_graph().Should().BeNull();
  42. var beforehand = tf.get_default_graph(); //this should create default automatically.
  43. beforehand.graph_key.Should().NotContain("0", "Already created a graph in an other thread.");
  44. tf.peak_default_graph().Should().NotBeNull();
  45. using (var sess = tf.Session())
  46. {
  47. var default_graph = tf.peak_default_graph();
  48. var sess_graph = sess.GetPrivate<Graph>("_graph");
  49. sess_graph.Should().NotBeNull();
  50. default_graph.Should().NotBeNull()
  51. .And.BeEquivalentTo(sess_graph)
  52. .And.BeEquivalentTo(beforehand);
  53. Console.WriteLine($"{tid}-{default_graph.graph_key}");
  54. //var result = sess.run(new object[] {g, a});
  55. //var actualDeriv = result[0].GetData<float>()[0];
  56. //var actual = result[1].GetData<float>()[0];
  57. }
  58. }
  59. }
  60. }
  61. }