TensorFlow自定义操作:输出张量的线程安全性

时间:2018-10-09 19:13:35

标签: c++ multithreading tensorflow

我有一个针对TensorFlow的自定义操作,该操作得益于并行进行某些计算。 但是,一个用户在使用此并行计算时报告了crash(在我的计算机上,一切正常)。

  • 使用线程时,在Compute方法中需要注意什么吗?
  • 读/写张量已经是线程安全的了吗?
  • 特别是,写入输出张量时是否必须同步线程?请参见下面的简化代码段。
    void Compute(OpKernelContext* context) override 
    {
        // output tensor
        Tensor* outputTensor = nullptr;
        OP_REQUIRES_OK(context, context->allocate_output(0, TensorShape({maxI, maxJ}), &outputTensor));
        auto outputMapped = outputTensor->tensor();

        // do some computations for index (i, j) and write result to index (i, j) of output tensor
        // do this for each (i, j) combination
        outputMapped(i, j) = result; 

        // computing the result for each index (i, j) in parallel would speed up the computation
        // Question: is it allowed to write to outputMapped(i, j) from different threads without synchronization?
    }

这是相关代码:

我浏览了TF docs,但只提到TF本身可以并行调用Compute(...)。但是我找不到有关张量的线程安全性的任何信息。有人对此有更多信息吗?

0 个答案:

没有答案