#NEWLINE# def c_code( self , node , nodename , inp , out , sub ) : #NEWLINE# #INDENT# ( x , b ) = inp #NEWLINE# ( z , ) = self #NEWLINE# fail = sub [ 'fail' ] #NEWLINE# return ( '\n        if (CudaNdarray_NDIM(%(x)s) != 2)\n        {\n            PyErr_SetString(PyExc_ValueError, "rank error input");\n            %(fail)s;\n        }\n        if (CudaNdarray_NDIM(%(b)s) != 1)\n        {\n            PyErr_SetString(PyExc_ValueError, "rank error for the bias");\n            %(fail)s;\n        }\n        if ((CudaNdarray_HOST_DIMS(%(x)s)[1] !=\n            CudaNdarray_HOST_DIMS(%(b)s)[0]))\n        {\n            PyErr_Format(PyExc_ValueError,\n                         "number of columns in x (%%ld)"\n                         " does not match length of b (%%ld)",\n                         (long int)CudaNdarray_HOST_DIMS(%(x)s)[1],\n                         (long int)CudaNdarray_HOST_DIMS(%(b)s)[0]);\n            %(fail)s;\n        }\n        if ((NULL == %(z)s)\n            || (CudaNdarray_HOST_DIMS(%(z)s)[0] !=\n                CudaNdarray_HOST_DIMS(%(x)s)[0])\n            || (CudaNdarray_HOST_DIMS(%(z)s)[1] !=\n                CudaNdarray_HOST_DIMS(%(x)s)[1]))\n        {\n            Py_XDECREF(%(z)s);\n            %(z)s = (CudaNdarray*)CudaNdarray_New();\n            if ((NULL == %(z)s)\n                || CudaNdarray_alloc_contiguous(%(z)s, 2,\n                       CudaNdarray_HOST_DIMS(%(x)s)))\n            {\n                Py_XDECREF(%(z)s);\n                %(z)s = NULL;\n                %(fail)s;\n            }\n        }\n        {\n            int n_blocks = std::min(CudaNdarray_HOST_DIMS(%(x)s)[0],32*1024);\n//TODO, detect the maximum number of thread per block.\n            int n_threads = std::min(CudaNdarray_HOST_DIMS(%(x)s)[1], 512);\n            int n_shared_bytes = CudaNdarray_HOST_DIMS(%(x)s)[1] *\n                                     2 * sizeof(float);\n            if (CudaNdarray_HOST_DIMS(%(x)s)[0] > 0)\n            {\n              if(n_shared_bytes < (32 * 1024 - 500)){\n                kSoftmaxWithBias_%(nodename)s\n                    <<<\n                        n_blocks,\n                        n_threads,\n                        n_shared_bytes\n                    >>>(\n                        CudaNdarray_HOST_DIMS(%(x)s)[0],\n                        CudaNdarray_HOST_DIMS(%(x)s)[1],\n\n                        CudaNdarray_DEV_DATA(%(x)s),\n                        CudaNdarray_HOST_STRIDES(%(x)s)[0],\n                        CudaNdarray_HOST_STRIDES(%(x)s)[1],\n\n                        CudaNdarray_DEV_DATA(%(b)s),\n                        CudaNdarray_HOST_STRIDES(%(b)s)[0],\n\n                        CudaNdarray_DEV_DATA(%(z)s),\n                        CudaNdarray_HOST_STRIDES(%(z)s)[0],\n                        CudaNdarray_HOST_STRIDES(%(z)s)[1]\n                    );\n              }else{\n                kSoftmaxWithBias_fixed_shared%(nodename)s\n                    <<<\n                        n_blocks,\n                        n_threads,\n                        n_threads * sizeof(float)\n                    >>>(\n                        CudaNdarray_HOST_DIMS(%(x)s)[0],\n                        CudaNdarray_HOST_DIMS(%(x)s)[1],\n\n                        CudaNdarray_DEV_DATA(%(x)s),\n                        CudaNdarray_HOST_STRIDES(%(x)s)[0],\n                        CudaNdarray_HOST_STRIDES(%(x)s)[1],\n\n                        CudaNdarray_DEV_DATA(%(b)s),\n                        CudaNdarray_HOST_STRIDES(%(b)s)[0],\n\n                        CudaNdarray_DEV_DATA(%(z)s),\n                        CudaNdarray_HOST_STRIDES(%(z)s)[0],\n                        CudaNdarray_HOST_STRIDES(%(z)s)[1]\n                    );\n              }\n                CNDA_THREAD_SYNC;\n                cudaError_t err = cudaGetLastError();\n                if( cudaSuccess != err)\n                {\n                    PyErr_Format(PyExc_RuntimeError,\n                                 "Cuda error: %%s: %%s. n_blocks=%%d,"\n                                 " n_threads=%%d, n_shared_bytes=%%d\\n",\n                                 "kSoftmaxWithBias_%(nodename)s",\n                                 cudaGetErrorString(err),\n                                 n_blocks, n_threads, n_shared_bytes);\n                    %(fail)s;\n                }\n            }\n        }\n        assert(%(z)s);\n        ' % locals ( ) )