Forward method engages only 1 CPU core when written in C++, while it engages multiple cores when written in Python

username: kovanostra
cooked: <p>Hello</p>
<p>I have a custom pytorch model of which I moved the forward method from python to C++ to speed it up. However, I notice that the pure Python version is engaging multiple cores while training, while the C++ version is engaging only 1 core.</p>
<p>I would like to ask if there is anything that specifically controls this particular behaviour and I need to make sure to add in my code or is it just that my C++ implementation is inefficient?</p>
<p><em>Code for both Python and C++ versions can be seen below:</em></p>
<p>Model in <strong>Python</strong>:<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/kovanostra/message-passing-neural-network/blob/master/message_passing_nn/model/graph_rnn_encoder.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/kovanostra/message-passing-neural-network/blob/master/message_passing_nn/model/graph_rnn_encoder.py" target="_blank" rel="nofollow noopener">kovanostra/message-passing-neural-network/blob/master/message_passing_nn/model/graph_rnn_encoder.py</a></h4>
<pre><code class="lang-py">from typing import List

import torch as to
import torch.nn as nn

from message_passing_nn.data.data_preprocessor import DataPreprocessor
from message_passing_nn.model.node import Node


class GraphRNNEncoder(nn.Module):
    def __init__(self,
                 time_steps: int,
                 number_of_nodes: int,
                 number_of_node_features: int,
                 fully_connected_layer_input_size: int,
                 fully_connected_layer_output_size: int,
                 device: str) -&gt; None:
        super(GraphRNNEncoder, self).__init__()
        node_features_tensor_shape = [number_of_node_features, number_of_node_features]
        nodes_tensor_shape = [number_of_nodes, number_of_nodes]
</code></pre>

  This file has been truncated. <a href="https://github.com/kovanostra/message-passing-neural-network/blob/master/message_passing_nn/model/graph_rnn_encoder.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>Forward/Backward methods in <strong>C++</strong>:<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/kovanostra/message-passing-neural-network/blob/v1.5.0/message_passing_nn/model/graph_rnn_encoder.cpp" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/kovanostra/message-passing-neural-network/blob/v1.5.0/message_passing_nn/model/graph_rnn_encoder.cpp" target="_blank" rel="nofollow noopener">kovanostra/message-passing-neural-network/blob/v1.5.0/message_passing_nn/model/graph_rnn_encoder.cpp</a></h4>
<pre><code class="lang-cpp">#include &lt;torch/extension.h&gt;
#include &lt;iostream&gt;

std::vector&lt;int&gt; find_nonzero_elements(const torch::Tensor&amp; tensor){
  std::vector&lt;int&gt; vector;
  for(int index=0; index&lt;tensor.sizes()[0]; index++){
    if(tensor[index].item&lt;int&gt;()!=0) {
       vector.push_back(index);
      }
  }
  return vector;
}

int find_index_by_value(const std::vector&lt;int&gt;&amp; vector, const int&amp; value){
  auto vector_size = static_cast&lt;int&gt;(vector.size());
  for (int index = 0; index &lt; vector_size; ++index) {
    if (vector[index]==value) return index;
  }
  return -1;
}
</code></pre>

  This file has been truncated. <a href="https://github.com/kovanostra/message-passing-neural-network/blob/v1.5.0/message_passing_nn/model/graph_rnn_encoder.cpp" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>