How does this C code (from lua library, Torch) even compile / work?

See https://github.com/torch/nn/blob/master/generic/Tanh.c

For example,

 static int nn_(Tanh_updateOutput)(lua_State *L)
{
   THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
   THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_Tensor);

   THTensor_(resizeAs)(output, input);

   if (input->nDimension == 1 || !THTensor_(isContiguous)(input) || !THTensor_(isContiguous)(output))
   {
    TH_TENSOR_APPLY2(real, output, real, input,   \
     *output_data = tanh(*input_data););
    }
  else
  {
   real* ptr_output = THTensor_(data)(output);
   real* ptr_input  = THTensor_(data)(input);
   long i;
 #pragma omp parallel for private(i)
for(i = 0; i < THTensor_(nElement)(input); i++)
  ptr_output[i] = tanh(ptr_input[i]);
}
return 1;
}

      

First, I don't know how to interpret the first line:

 static int nn_(Tanh_updateOutput)(lua_State *L)

      

What are the arguments here? What is Tanh_updateOutput talking about? Does "nn_" have a special meaning?

Second, "TH_TENSOR_APPLY2" and "THTensor _ (...)" are both used, but I can't see where they are defined? There are no other inclusions in this file?

+3


source to share


1 answer


nn_

- macro. You can find the definition by searching the repository for "#define nn_"

; it is in init.c

:

#define nn_(NAME) TH_CONCAT_3(nn_, Real, NAME)

      



You can follow the chaining of macros and you will probably end up adding some sort of bullet insertion that expands nn_(Tanh_updateOutput)

to the function name.

(Strange that it generic/Tanh.c

does not contain any inclusions; generic/Tanh.c

must be included by some other file. This is unusual for files .c

.)

+7


source







All Articles