text:
# Julia wrapper for header: /home/shindo/cudnn.h
# Automatically generated using Clang.jl wrap_c, version 0.0.0

function cudnnGetVersion()
    ccall((:cudnnGetVersion,libcudnn),Cint,())
end

function cudnnGetErrorString(status)
    ccall((:cudnnGetErrorString,libcudnn),Cstring,(cudnnStatus_t,),status)
end

function cudnnCreate(handle)
    checkstatus(ccall((:cudnnCreate,libcudnn),cudnnStatus_t,(Ptr{cudnnHandle_t},),handle))
end

function cudnnDestroy(handle)
    checkstatus(ccall((:cudnnDestroy,libcudnn),cudnnStatus_t,(cudnnHandle_t,),handle))
end

function cudnnSetStream(handle,streamId)
    checkstatus(ccall((:cudnnSetStream,libcudnn),cudnnStatus_t,(cudnnHandle_t,Cint),handle,streamId))
end

function cudnnGetStream(handle,streamId)
    checkstatus(ccall((:cudnnGetStream,libcudnn),cudnnStatus_t,(cudnnHandle_t,Ptr{Cint}),handle,streamId))
end

function cudnnCreateTensorDescriptor(tensorDesc)
    checkstatus(ccall((:cudnnCreateTensorDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnTensorDescriptor_t},),tensorDesc))
end

function cudnnSetTensor4dDescriptor(tensorDesc,format,dataType,n,c,h,w)
    checkstatus(ccall((:cudnnSetTensor4dDescriptor,libcudnn),cudnnStatus_t,(cudnnTensorDescriptor_t,cudnnTensorFormat_t,cudnnDataType_t,Cint,Cint,Cint,Cint),tensorDesc,format,dataType,n,c,h,w))
end

function cudnnSetTensor4dDescriptorEx(tensorDesc,dataType,n,c,h,w,nStride,cStride,hStride,wStride)
    checkstatus(ccall((:cudnnSetTensor4dDescriptorEx,libcudnn),cudnnStatus_t,(cudnnTensorDescriptor_t,cudnnDataType_t,Cint,Cint,Cint,Cint,Cint,Cint,Cint,Cint),tensorDesc,dataType,n,c,h,w,nStride,cStride,hStride,wStride))
end

function cudnnGetTensor4dDescriptor(tensorDesc,dataType,n,c,h,w,nStride,cStride,hStride,wStride)
    checkstatus(ccall((:cudnnGetTensor4dDescriptor,libcudnn),cudnnStatus_t,(cudnnTensorDescriptor_t,Ptr{cudnnDataType_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),tensorDesc,dataType,n,c,h,w,nStride,cStride,hStride,wStride))
end

function cudnnSetTensorNdDescriptor(tensorDesc,dataType,nbDims,dimA,strideA)
    checkstatus(ccall((:cudnnSetTensorNdDescriptor,libcudnn),cudnnStatus_t,(cudnnTensorDescriptor_t,cudnnDataType_t,Cint,Ptr{Cint},Ptr{Cint}),tensorDesc,dataType,nbDims,dimA,strideA))
end

function cudnnGetTensorNdDescriptor(tensorDesc,nbDimsRequested,dataType,nbDims,dimA,strideA)
    checkstatus(ccall((:cudnnGetTensorNdDescriptor,libcudnn),cudnnStatus_t,(cudnnTensorDescriptor_t,Cint,Ptr{cudnnDataType_t},Ptr{Cint},Ptr{Cint},Ptr{Cint}),tensorDesc,nbDimsRequested,dataType,nbDims,dimA,strideA))
end

function cudnnDestroyTensorDescriptor(tensorDesc)
    checkstatus(ccall((:cudnnDestroyTensorDescriptor,libcudnn),cudnnStatus_t,(cudnnTensorDescriptor_t,),tensorDesc))
end

function cudnnTransformTensor(handle,alpha,xDesc,x,beta,yDesc,y)
    checkstatus(ccall((:cudnnTransformTensor,libcudnn),cudnnStatus_t,(cudnnHandle_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,alpha,xDesc,x,beta,yDesc,y))
end

function cudnnAddTensor(handle,alpha,aDesc,A,beta,cDesc,C)
    checkstatus(ccall((:cudnnAddTensor,libcudnn),cudnnStatus_t,(cudnnHandle_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,alpha,aDesc,A,beta,cDesc,C))
end

function cudnnCreateOpTensorDescriptor(opTensorDesc)
    checkstatus(ccall((:cudnnCreateOpTensorDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnOpTensorDescriptor_t},),opTensorDesc))
end

function cudnnSetOpTensorDescriptor(opTensorDesc,opTensorOp,opTensorCompType,opTensorNanOpt)
    checkstatus(ccall((:cudnnSetOpTensorDescriptor,libcudnn),cudnnStatus_t,(cudnnOpTensorDescriptor_t,cudnnOpTensorOp_t,cudnnDataType_t,cudnnNanPropagation_t),opTensorDesc,opTensorOp,opTensorCompType,opTensorNanOpt))
end

function cudnnGetOpTensorDescriptor(opTensorDesc,opTensorOp,opTensorCompType,opTensorNanOpt)
    checkstatus(ccall((:cudnnGetOpTensorDescriptor,libcudnn),cudnnStatus_t,(cudnnOpTensorDescriptor_t,Ptr{cudnnOpTensorOp_t},Ptr{cudnnDataType_t},Ptr{cudnnNanPropagation_t}),opTensorDesc,opTensorOp,opTensorCompType,opTensorNanOpt))
end

function cudnnDestroyOpTensorDescriptor(opTensorDesc)
    checkstatus(ccall((:cudnnDestroyOpTensorDescriptor,libcudnn),cudnnStatus_t,(cudnnOpTensorDescriptor_t,),opTensorDesc))
end

function cudnnOpTensor(handle,opTensorDesc,alpha1,aDesc,A,alpha2,bDesc,B,beta,cDesc,C)
    checkstatus(ccall((:cudnnOpTensor,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnOpTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,opTensorDesc,alpha1,aDesc,A,alpha2,bDesc,B,beta,cDesc,C))
end

function cudnnSetTensor(handle,yDesc,y,valuePtr)
    checkstatus(ccall((:cudnnSetTensor,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void}),handle,yDesc,y,valuePtr))
end

function cudnnScaleTensor(handle,yDesc,y,alpha)
    checkstatus(ccall((:cudnnScaleTensor,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void}),handle,yDesc,y,alpha))
end

function cudnnCreateFilterDescriptor(filterDesc)
    checkstatus(ccall((:cudnnCreateFilterDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnFilterDescriptor_t},),filterDesc))
end

function cudnnSetFilter4dDescriptor(filterDesc,dataType,format,k,c,h,w)
    checkstatus(ccall((:cudnnSetFilter4dDescriptor,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,cudnnDataType_t,cudnnTensorFormat_t,Cint,Cint,Cint,Cint),filterDesc,dataType,format,k,c,h,w))
end

function cudnnGetFilter4dDescriptor(filterDesc,dataType,format,k,c,h,w)
    checkstatus(ccall((:cudnnGetFilter4dDescriptor,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,Ptr{cudnnDataType_t},Ptr{cudnnTensorFormat_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),filterDesc,dataType,format,k,c,h,w))
end

function cudnnSetFilterNdDescriptor(filterDesc,dataType,format,nbDims,filterDimA)
    checkstatus(ccall((:cudnnSetFilterNdDescriptor,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,cudnnDataType_t,cudnnTensorFormat_t,Cint,Ptr{Cint}),filterDesc,dataType,format,nbDims,filterDimA))
end

function cudnnGetFilterNdDescriptor(filterDesc,nbDimsRequested,dataType,format,nbDims,filterDimA)
    checkstatus(ccall((:cudnnGetFilterNdDescriptor,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,Cint,Ptr{cudnnDataType_t},Ptr{cudnnTensorFormat_t},Ptr{Cint},Ptr{Cint}),filterDesc,nbDimsRequested,dataType,format,nbDims,filterDimA))
end

function cudnnDestroyFilterDescriptor(filterDesc)
    checkstatus(ccall((:cudnnDestroyFilterDescriptor,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,),filterDesc))
end

function cudnnCreateConvolutionDescriptor(convDesc)
    checkstatus(ccall((:cudnnCreateConvolutionDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnConvolutionDescriptor_t},),convDesc))
end

function cudnnSetConvolution2dDescriptor(convDesc,pad_h,pad_w,u,v,upscalex,upscaley,mode)
    checkstatus(ccall((:cudnnSetConvolution2dDescriptor,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,Cint,Cint,Cint,Cint,Cint,Cint,cudnnConvolutionMode_t),convDesc,pad_h,pad_w,u,v,upscalex,upscaley,mode))
end

function cudnnSetConvolution2dDescriptor_v5(convDesc,pad_h,pad_w,u,v,upscalex,upscaley,mode,dataType)
    checkstatus(ccall((:cudnnSetConvolution2dDescriptor_v5,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,Cint,Cint,Cint,Cint,Cint,Cint,cudnnConvolutionMode_t,cudnnDataType_t),convDesc,pad_h,pad_w,u,v,upscalex,upscaley,mode,dataType))
end

function cudnnGetConvolution2dDescriptor(convDesc,pad_h,pad_w,u,v,upscalex,upscaley,mode)
    checkstatus(ccall((:cudnnGetConvolution2dDescriptor,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{cudnnConvolutionMode_t}),convDesc,pad_h,pad_w,u,v,upscalex,upscaley,mode))
end

function cudnnGetConvolution2dDescriptor_v5(convDesc,pad_h,pad_w,u,v,upscalex,upscaley,mode,dataType)
    checkstatus(ccall((:cudnnGetConvolution2dDescriptor_v5,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{cudnnConvolutionMode_t},Ptr{cudnnDataType_t}),convDesc,pad_h,pad_w,u,v,upscalex,upscaley,mode,dataType))
end

function cudnnGetConvolution2dForwardOutputDim(convDesc,inputTensorDesc,filterDesc,n,c,h,w)
    checkstatus(ccall((:cudnnGetConvolution2dForwardOutputDim,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,cudnnFilterDescriptor_t,Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),convDesc,inputTensorDesc,filterDesc,n,c,h,w))
end

function cudnnSetConvolutionNdDescriptor(convDesc,arrayLength,padA,filterStrideA,upscaleA,mode,dataType)
    checkstatus(ccall((:cudnnSetConvolutionNdDescriptor,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,Cint,Ptr{Cint},Ptr{Cint},Ptr{Cint},cudnnConvolutionMode_t,cudnnDataType_t),convDesc,arrayLength,padA,filterStrideA,upscaleA,mode,dataType))
end

function cudnnGetConvolutionNdDescriptor(convDesc,arrayLengthRequested,arrayLength,padA,strideA,upscaleA,mode,dataType)
    checkstatus(ccall((:cudnnGetConvolutionNdDescriptor,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,Cint,Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{cudnnConvolutionMode_t},Ptr{cudnnDataType_t}),convDesc,arrayLengthRequested,arrayLength,padA,strideA,upscaleA,mode,dataType))
end

function cudnnGetConvolutionNdForwardOutputDim(convDesc,inputTensorDesc,filterDesc,nbDims,tensorOuputDimA)
    checkstatus(ccall((:cudnnGetConvolutionNdForwardOutputDim,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,cudnnFilterDescriptor_t,Cint,Ptr{Cint}),convDesc,inputTensorDesc,filterDesc,nbDims,tensorOuputDimA))
end

function cudnnDestroyConvolutionDescriptor(convDesc)
    checkstatus(ccall((:cudnnDestroyConvolutionDescriptor,libcudnn),cudnnStatus_t,(cudnnConvolutionDescriptor_t,),convDesc))
end

function cudnnFindConvolutionForwardAlgorithm(handle,xDesc,wDesc,convDesc,yDesc,requestedAlgoCount,returnedAlgoCount,perfResults)
    checkstatus(ccall((:cudnnFindConvolutionForwardAlgorithm,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,cudnnFilterDescriptor_t,cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,Cint,Ptr{Cint},Ptr{cudnnConvolutionFwdAlgoPerf_t}),handle,xDesc,wDesc,convDesc,yDesc,requestedAlgoCount,returnedAlgoCount,perfResults))
end

function cudnnFindConvolutionForwardAlgorithmEx(handle,xDesc,x,wDesc,w,convDesc,yDesc,y,requestedAlgoCount,returnedAlgoCount,perfResults,workSpace,workSpaceSizeInBytes)
    checkstatus(ccall((:cudnnFindConvolutionForwardAlgorithmEx,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,Ptr{Void},cudnnFilterDescriptor_t,Ptr{Void},cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,Ptr{Void},Cint,Ptr{Cint},Ptr{cudnnConvolutionFwdAlgoPerf_t},Ptr{Void},Cint),handle,xDesc,x,wDesc,w,convDesc,yDesc,y,requestedAlgoCount,returnedAlgoCount,perfResults,workSpace,workSpaceSizeInBytes))
end

function cudnnGetConvolutionForwardAlgorithm(handle,xDesc,wDesc,convDesc,yDesc,preference,memoryLimitInBytes,algo)
    checkstatus(ccall((:cudnnGetConvolutionForwardAlgorithm,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,cudnnFilterDescriptor_t,cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionFwdPreference_t,Cint,Ptr{cudnnConvolutionFwdAlgo_t}),handle,xDesc,wDesc,convDesc,yDesc,preference,memoryLimitInBytes,algo))
end

function cudnnGetConvolutionForwardWorkspaceSize(handle,xDesc,wDesc,convDesc,yDesc,algo,sizeInBytes)
    checkstatus(ccall((:cudnnGetConvolutionForwardWorkspaceSize,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,cudnnFilterDescriptor_t,cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionFwdAlgo_t,Ptr{Cint}),handle,xDesc,wDesc,convDesc,yDesc,algo,sizeInBytes))
end

function cudnnConvolutionForward(handle,alpha,xDesc,x,wDesc,w,convDesc,algo,workSpace,workSpaceSizeInBytes,beta,yDesc,y)
    checkstatus(ccall((:cudnnConvolutionForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnFilterDescriptor_t,Ptr{Void},cudnnConvolutionDescriptor_t,cudnnConvolutionFwdAlgo_t,Ptr{Void},Cint,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,alpha,xDesc,x,wDesc,w,convDesc,algo,workSpace,workSpaceSizeInBytes,beta,yDesc,y))
end

function cudnnConvolutionBackwardBias(handle,alpha,dyDesc,dy,beta,dbDesc,db)
    checkstatus(ccall((:cudnnConvolutionBackwardBias,libcudnn),cudnnStatus_t,(cudnnHandle_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,alpha,dyDesc,dy,beta,dbDesc,db))
end

function cudnnFindConvolutionBackwardFilterAlgorithm(handle,xDesc,dyDesc,convDesc,dwDesc,requestedAlgoCount,returnedAlgoCount,perfResults)
    checkstatus(ccall((:cudnnFindConvolutionBackwardFilterAlgorithm,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionDescriptor_t,cudnnFilterDescriptor_t,Cint,Ptr{Cint},Ptr{cudnnConvolutionBwdFilterAlgoPerf_t}),handle,xDesc,dyDesc,convDesc,dwDesc,requestedAlgoCount,returnedAlgoCount,perfResults))
end

function cudnnFindConvolutionBackwardFilterAlgorithmEx(handle,xDesc,x,dyDesc,y,convDesc,dwDesc,dw,requestedAlgoCount,returnedAlgoCount,perfResults,workSpace,workSpaceSizeInBytes)
    checkstatus(ccall((:cudnnFindConvolutionBackwardFilterAlgorithmEx,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnConvolutionDescriptor_t,cudnnFilterDescriptor_t,Ptr{Void},Cint,Ptr{Cint},Ptr{cudnnConvolutionBwdFilterAlgoPerf_t},Ptr{Void},Cint),handle,xDesc,x,dyDesc,y,convDesc,dwDesc,dw,requestedAlgoCount,returnedAlgoCount,perfResults,workSpace,workSpaceSizeInBytes))
end

function cudnnGetConvolutionBackwardFilterAlgorithm(handle,xDesc,dyDesc,convDesc,dwDesc,preference,memoryLimitInBytes,algo)
    checkstatus(ccall((:cudnnGetConvolutionBackwardFilterAlgorithm,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionDescriptor_t,cudnnFilterDescriptor_t,cudnnConvolutionBwdFilterPreference_t,Cint,Ptr{cudnnConvolutionBwdFilterAlgo_t}),handle,xDesc,dyDesc,convDesc,dwDesc,preference,memoryLimitInBytes,algo))
end

function cudnnGetConvolutionBackwardFilterWorkspaceSize(handle,xDesc,dyDesc,convDesc,gradDesc,algo,sizeInBytes)
    checkstatus(ccall((:cudnnGetConvolutionBackwardFilterWorkspaceSize,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionDescriptor_t,cudnnFilterDescriptor_t,cudnnConvolutionBwdFilterAlgo_t,Ptr{Cint}),handle,xDesc,dyDesc,convDesc,gradDesc,algo,sizeInBytes))
end

function cudnnConvolutionBackwardFilter(handle,alpha,xDesc,x,dyDesc,dy,convDesc,algo,workSpace,workSpaceSizeInBytes,beta,dwDesc,dw)
    checkstatus(ccall((:cudnnConvolutionBackwardFilter,libcudnn),cudnnStatus_t,(cudnnHandle_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnConvolutionDescriptor_t,cudnnConvolutionBwdFilterAlgo_t,Ptr{Void},Cint,Ptr{Void},cudnnFilterDescriptor_t,Ptr{Void}),handle,alpha,xDesc,x,dyDesc,dy,convDesc,algo,workSpace,workSpaceSizeInBytes,beta,dwDesc,dw))
end

function cudnnFindConvolutionBackwardDataAlgorithm(handle,wDesc,dyDesc,convDesc,dxDesc,requestedAlgoCount,returnedAlgoCount,perfResults)
    checkstatus(ccall((:cudnnFindConvolutionBackwardDataAlgorithm,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnFilterDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,Cint,Ptr{Cint},Ptr{cudnnConvolutionBwdDataAlgoPerf_t}),handle,wDesc,dyDesc,convDesc,dxDesc,requestedAlgoCount,returnedAlgoCount,perfResults))
end

function cudnnFindConvolutionBackwardDataAlgorithmEx(handle,wDesc,w,dyDesc,dy,convDesc,dxDesc,dx,requestedAlgoCount,returnedAlgoCount,perfResults,workSpace,workSpaceSizeInBytes)
    checkstatus(ccall((:cudnnFindConvolutionBackwardDataAlgorithmEx,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnFilterDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,Ptr{Void},Cint,Ptr{Cint},Ptr{cudnnConvolutionBwdDataAlgoPerf_t},Ptr{Void},Cint),handle,wDesc,w,dyDesc,dy,convDesc,dxDesc,dx,requestedAlgoCount,returnedAlgoCount,perfResults,workSpace,workSpaceSizeInBytes))
end

function cudnnGetConvolutionBackwardDataAlgorithm(handle,wDesc,dyDesc,convDesc,dxDesc,preference,memoryLimitInBytes,algo)
    checkstatus(ccall((:cudnnGetConvolutionBackwardDataAlgorithm,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnFilterDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionBwdDataPreference_t,Cint,Ptr{cudnnConvolutionBwdDataAlgo_t}),handle,wDesc,dyDesc,convDesc,dxDesc,preference,memoryLimitInBytes,algo))
end

function cudnnGetConvolutionBackwardDataWorkspaceSize(handle,wDesc,dyDesc,convDesc,dxDesc,algo,sizeInBytes)
    checkstatus(ccall((:cudnnGetConvolutionBackwardDataWorkspaceSize,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnFilterDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionDescriptor_t,cudnnTensorDescriptor_t,cudnnConvolutionBwdDataAlgo_t,Ptr{Cint}),handle,wDesc,dyDesc,convDesc,dxDesc,algo,sizeInBytes))
end

function cudnnConvolutionBackwardData(handle,alpha,wDesc,w,dyDesc,dy,convDesc,algo,workSpace,workSpaceSizeInBytes,beta,dxDesc,dx)
    checkstatus(ccall((:cudnnConvolutionBackwardData,libcudnn),cudnnStatus_t,(cudnnHandle_t,Ptr{Void},cudnnFilterDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnConvolutionDescriptor_t,cudnnConvolutionBwdDataAlgo_t,Ptr{Void},Cint,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,alpha,wDesc,w,dyDesc,dy,convDesc,algo,workSpace,workSpaceSizeInBytes,beta,dxDesc,dx))
end

function cudnnIm2Col(handle,xDesc,x,wDesc,convDesc,colBuffer)
    checkstatus(ccall((:cudnnIm2Col,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnTensorDescriptor_t,Ptr{Void},cudnnFilterDescriptor_t,cudnnConvolutionDescriptor_t,Ptr{Void}),handle,xDesc,x,wDesc,convDesc,colBuffer))
end

function cudnnSoftmaxForward(handle,algo,mode,alpha,xDesc,x,beta,yDesc,y)
    checkstatus(ccall((:cudnnSoftmaxForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnSoftmaxAlgorithm_t,cudnnSoftmaxMode_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,algo,mode,alpha,xDesc,x,beta,yDesc,y))
end

function cudnnSoftmaxBackward(handle,algo,mode,alpha,yDesc,y,dyDesc,dy,beta,dxDesc,dx)
    checkstatus(ccall((:cudnnSoftmaxBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnSoftmaxAlgorithm_t,cudnnSoftmaxMode_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,algo,mode,alpha,yDesc,y,dyDesc,dy,beta,dxDesc,dx))
end

function cudnnCreatePoolingDescriptor(poolingDesc)
    checkstatus(ccall((:cudnnCreatePoolingDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnPoolingDescriptor_t},),poolingDesc))
end

function cudnnSetPooling2dDescriptor(poolingDesc,mode,maxpoolingNanOpt,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride)
    checkstatus(ccall((:cudnnSetPooling2dDescriptor,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,cudnnPoolingMode_t,cudnnNanPropagation_t,Cint,Cint,Cint,Cint,Cint,Cint),poolingDesc,mode,maxpoolingNanOpt,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride))
end

function cudnnGetPooling2dDescriptor(poolingDesc,mode,maxpoolingNanOpt,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride)
    checkstatus(ccall((:cudnnGetPooling2dDescriptor,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,Ptr{cudnnPoolingMode_t},Ptr{cudnnNanPropagation_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,mode,maxpoolingNanOpt,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride))
end

function cudnnSetPoolingNdDescriptor(poolingDesc,mode,maxpoolingNanOpt,nbDims,windowDimA,paddingA,strideA)
    checkstatus(ccall((:cudnnSetPoolingNdDescriptor,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,cudnnPoolingMode_t,cudnnNanPropagation_t,Cint,Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,mode,maxpoolingNanOpt,nbDims,windowDimA,paddingA,strideA))
end

function cudnnGetPoolingNdDescriptor(poolingDesc,nbDimsRequested,mode,maxpoolingNanOpt,nbDims,windowDimA,paddingA,strideA)
    checkstatus(ccall((:cudnnGetPoolingNdDescriptor,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,Cint,Ptr{cudnnPoolingMode_t},Ptr{cudnnNanPropagation_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,nbDimsRequested,mode,maxpoolingNanOpt,nbDims,windowDimA,paddingA,strideA))
end

function cudnnGetPoolingNdForwardOutputDim(poolingDesc,inputTensorDesc,nbDims,outputTensorDimA)
    checkstatus(ccall((:cudnnGetPoolingNdForwardOutputDim,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,cudnnTensorDescriptor_t,Cint,Ptr{Cint}),poolingDesc,inputTensorDesc,nbDims,outputTensorDimA))
end

function cudnnGetPooling2dForwardOutputDim(poolingDesc,inputTensorDesc,n,c,h,w)
    checkstatus(ccall((:cudnnGetPooling2dForwardOutputDim,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,cudnnTensorDescriptor_t,Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,inputTensorDesc,n,c,h,w))
end

function cudnnDestroyPoolingDescriptor(poolingDesc)
    checkstatus(ccall((:cudnnDestroyPoolingDescriptor,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,),poolingDesc))
end

function cudnnPoolingForward(handle,poolingDesc,alpha,xDesc,x,beta,yDesc,y)
    checkstatus(ccall((:cudnnPoolingForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnPoolingDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,poolingDesc,alpha,xDesc,x,beta,yDesc,y))
end

function cudnnPoolingBackward(handle,poolingDesc,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx)
    checkstatus(ccall((:cudnnPoolingBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnPoolingDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,poolingDesc,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx))
end

function cudnnCreateActivationDescriptor(activationDesc)
    checkstatus(ccall((:cudnnCreateActivationDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnActivationDescriptor_t},),activationDesc))
end

function cudnnSetActivationDescriptor(activationDesc,mode,reluNanOpt,reluCeiling)
    checkstatus(ccall((:cudnnSetActivationDescriptor,libcudnn),cudnnStatus_t,(cudnnActivationDescriptor_t,cudnnActivationMode_t,cudnnNanPropagation_t,Cdouble),activationDesc,mode,reluNanOpt,reluCeiling))
end

function cudnnGetActivationDescriptor(activationDesc,mode,reluNanOpt,reluCeiling)
    checkstatus(ccall((:cudnnGetActivationDescriptor,libcudnn),cudnnStatus_t,(cudnnActivationDescriptor_t,Ptr{cudnnActivationMode_t},Ptr{cudnnNanPropagation_t},Ptr{Cdouble}),activationDesc,mode,reluNanOpt,reluCeiling))
end

function cudnnDestroyActivationDescriptor(activationDesc)
    checkstatus(ccall((:cudnnDestroyActivationDescriptor,libcudnn),cudnnStatus_t,(cudnnActivationDescriptor_t,),activationDesc))
end

function cudnnActivationForward(handle,activationDesc,alpha,xDesc,x,beta,yDesc,y)
    checkstatus(ccall((:cudnnActivationForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnActivationDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,activationDesc,alpha,xDesc,x,beta,yDesc,y))
end

function cudnnActivationBackward(handle,activationDesc,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx)
    checkstatus(ccall((:cudnnActivationBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnActivationDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,activationDesc,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx))
end

function cudnnCreateLRNDescriptor(normDesc)
    checkstatus(ccall((:cudnnCreateLRNDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnLRNDescriptor_t},),normDesc))
end

function cudnnSetLRNDescriptor(normDesc,lrnN,lrnAlpha,lrnBeta,lrnK)
    checkstatus(ccall((:cudnnSetLRNDescriptor,libcudnn),cudnnStatus_t,(cudnnLRNDescriptor_t,UInt32,Cdouble,Cdouble,Cdouble),normDesc,lrnN,lrnAlpha,lrnBeta,lrnK))
end

function cudnnGetLRNDescriptor(normDesc,lrnN,lrnAlpha,lrnBeta,lrnK)
    checkstatus(ccall((:cudnnGetLRNDescriptor,libcudnn),cudnnStatus_t,(cudnnLRNDescriptor_t,Ptr{UInt32},Ptr{Cdouble},Ptr{Cdouble},Ptr{Cdouble}),normDesc,lrnN,lrnAlpha,lrnBeta,lrnK))
end

function cudnnDestroyLRNDescriptor(lrnDesc)
    checkstatus(ccall((:cudnnDestroyLRNDescriptor,libcudnn),cudnnStatus_t,(cudnnLRNDescriptor_t,),lrnDesc))
end

function cudnnLRNCrossChannelForward(handle,normDesc,lrnMode,alpha,xDesc,x,beta,yDesc,y)
    checkstatus(ccall((:cudnnLRNCrossChannelForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnLRNDescriptor_t,cudnnLRNMode_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,normDesc,lrnMode,alpha,xDesc,x,beta,yDesc,y))
end

function cudnnLRNCrossChannelBackward(handle,normDesc,lrnMode,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx)
    checkstatus(ccall((:cudnnLRNCrossChannelBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnLRNDescriptor_t,cudnnLRNMode_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,normDesc,lrnMode,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx))
end

function cudnnDivisiveNormalizationForward(handle,normDesc,mode,alpha,xDesc,x,means,temp,temp2,beta,yDesc,y)
    checkstatus(ccall((:cudnnDivisiveNormalizationForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnLRNDescriptor_t,cudnnDivNormMode_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Ptr{Void},Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,normDesc,mode,alpha,xDesc,x,means,temp,temp2,beta,yDesc,y))
end

function cudnnDivisiveNormalizationBackward(handle,normDesc,mode,alpha,xDesc,x,means,dy,temp,temp2,beta,dXdMeansDesc,dx,dMeans)
    checkstatus(ccall((:cudnnDivisiveNormalizationBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnLRNDescriptor_t,cudnnDivNormMode_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Ptr{Void},Ptr{Void},Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void}),handle,normDesc,mode,alpha,xDesc,x,means,dy,temp,temp2,beta,dXdMeansDesc,dx,dMeans))
end

function cudnnDeriveBNTensorDescriptor(derivedBnDesc,xDesc,mode)
    checkstatus(ccall((:cudnnDeriveBNTensorDescriptor,libcudnn),cudnnStatus_t,(cudnnTensorDescriptor_t,cudnnTensorDescriptor_t,cudnnBatchNormMode_t),derivedBnDesc,xDesc,mode))
end

function cudnnBatchNormalizationForwardTraining(handle,mode,alpha,beta,xDesc,x,yDesc,y,bnScaleBiasMeanVarDesc,bnScale,bnBias,exponentialAverageFactor,resultRunningMean,resultRunningVariance,epsilon,resultSaveMean,resultSaveInvVariance)
    checkstatus(ccall((:cudnnBatchNormalizationForwardTraining,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnBatchNormMode_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Cdouble,Ptr{Void},Ptr{Void},Cdouble,Ptr{Void},Ptr{Void}),handle,mode,alpha,beta,xDesc,x,yDesc,y,bnScaleBiasMeanVarDesc,bnScale,bnBias,exponentialAverageFactor,resultRunningMean,resultRunningVariance,epsilon,resultSaveMean,resultSaveInvVariance))
end

function cudnnBatchNormalizationForwardInference(handle,mode,alpha,beta,xDesc,x,yDesc,y,bnScaleBiasMeanVarDesc,bnScale,bnBias,estimatedMean,estimatedVariance,epsilon)
    checkstatus(ccall((:cudnnBatchNormalizationForwardInference,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnBatchNormMode_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Ptr{Void},Ptr{Void},Cdouble),handle,mode,alpha,beta,xDesc,x,yDesc,y,bnScaleBiasMeanVarDesc,bnScale,bnBias,estimatedMean,estimatedVariance,epsilon))
end

function cudnnBatchNormalizationBackward(handle,mode,alphaDataDiff,betaDataDiff,alphaParamDiff,betaParamDiff,xDesc,x,dyDesc,dy,dxDesc,dx,dBnScaleBiasDesc,bnScale,dBnScaleResult,dBnBiasResult,epsilon,savedMean,savedInvVariance)
    checkstatus(ccall((:cudnnBatchNormalizationBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnBatchNormMode_t,Ptr{Void},Ptr{Void},Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Ptr{Void},Cdouble,Ptr{Void},Ptr{Void}),handle,mode,alphaDataDiff,betaDataDiff,alphaParamDiff,betaParamDiff,xDesc,x,dyDesc,dy,dxDesc,dx,dBnScaleBiasDesc,bnScale,dBnScaleResult,dBnBiasResult,epsilon,savedMean,savedInvVariance))
end

function cudnnCreateSpatialTransformerDescriptor(stDesc)
    checkstatus(ccall((:cudnnCreateSpatialTransformerDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnSpatialTransformerDescriptor_t},),stDesc))
end

function cudnnSetSpatialTransformerNdDescriptor(stDesc,samplerType,dataType,nbDims,dimA)
    checkstatus(ccall((:cudnnSetSpatialTransformerNdDescriptor,libcudnn),cudnnStatus_t,(cudnnSpatialTransformerDescriptor_t,cudnnSamplerType_t,cudnnDataType_t,Cint,Ptr{Cint}),stDesc,samplerType,dataType,nbDims,dimA))
end

function cudnnDestroySpatialTransformerDescriptor(stDesc)
    checkstatus(ccall((:cudnnDestroySpatialTransformerDescriptor,libcudnn),cudnnStatus_t,(cudnnSpatialTransformerDescriptor_t,),stDesc))
end

function cudnnSpatialTfGridGeneratorForward(handle,stDesc,theta,grid)
    checkstatus(ccall((:cudnnSpatialTfGridGeneratorForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnSpatialTransformerDescriptor_t,Ptr{Void},Ptr{Void}),handle,stDesc,theta,grid))
end

function cudnnSpatialTfGridGeneratorBackward(handle,stDesc,dgrid,dtheta)
    checkstatus(ccall((:cudnnSpatialTfGridGeneratorBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnSpatialTransformerDescriptor_t,Ptr{Void},Ptr{Void}),handle,stDesc,dgrid,dtheta))
end

function cudnnSpatialTfSamplerForward(handle,stDesc,alpha,xDesc,x,grid,beta,yDesc,y)
    checkstatus(ccall((:cudnnSpatialTfSamplerForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnSpatialTransformerDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,stDesc,alpha,xDesc,x,grid,beta,yDesc,y))
end

function cudnnSpatialTfSamplerBackward(handle,stDesc,alpha,xDesc,x,beta,dxDesc,dx,alphaDgrid,dyDesc,dy,grid,betaDgrid,dgrid)
    checkstatus(ccall((:cudnnSpatialTfSamplerBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnSpatialTransformerDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Ptr{Void},Ptr{Void}),handle,stDesc,alpha,xDesc,x,beta,dxDesc,dx,alphaDgrid,dyDesc,dy,grid,betaDgrid,dgrid))
end

function cudnnCreateDropoutDescriptor(dropoutDesc)
    checkstatus(ccall((:cudnnCreateDropoutDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnDropoutDescriptor_t},),dropoutDesc))
end

function cudnnDestroyDropoutDescriptor(dropoutDesc)
    checkstatus(ccall((:cudnnDestroyDropoutDescriptor,libcudnn),cudnnStatus_t,(cudnnDropoutDescriptor_t,),dropoutDesc))
end

function cudnnDropoutGetStatesSize(handle,sizeInBytes)
    checkstatus(ccall((:cudnnDropoutGetStatesSize,libcudnn),cudnnStatus_t,(cudnnHandle_t,Ptr{Cint}),handle,sizeInBytes))
end

function cudnnDropoutGetReserveSpaceSize(xdesc,sizeInBytes)
    checkstatus(ccall((:cudnnDropoutGetReserveSpaceSize,libcudnn),cudnnStatus_t,(cudnnTensorDescriptor_t,Ptr{Cint}),xdesc,sizeInBytes))
end

function cudnnSetDropoutDescriptor(dropoutDesc,handle,dropout,states,stateSizeInBytes,seed)
    checkstatus(ccall((:cudnnSetDropoutDescriptor,libcudnn),cudnnStatus_t,(cudnnDropoutDescriptor_t,cudnnHandle_t,Cfloat,Ptr{Void},Cint,Culonglong),dropoutDesc,handle,dropout,states,stateSizeInBytes,seed))
end

function cudnnDropoutForward(handle,dropoutDesc,xdesc,x,ydesc,y,reserveSpace,reserveSpaceSizeInBytes)
    checkstatus(ccall((:cudnnDropoutForward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnDropoutDescriptor_t,cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Cint),handle,dropoutDesc,xdesc,x,ydesc,y,reserveSpace,reserveSpaceSizeInBytes))
end

function cudnnDropoutBackward(handle,dropoutDesc,dydesc,dy,dxdesc,dx,reserveSpace,reserveSpaceSizeInBytes)
    checkstatus(ccall((:cudnnDropoutBackward,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnDropoutDescriptor_t,cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Cint),handle,dropoutDesc,dydesc,dy,dxdesc,dx,reserveSpace,reserveSpaceSizeInBytes))
end

function cudnnCreateRNNDescriptor(rnnDesc)
    checkstatus(ccall((:cudnnCreateRNNDescriptor,libcudnn),cudnnStatus_t,(Ptr{cudnnRNNDescriptor_t},),rnnDesc))
end

function cudnnDestroyRNNDescriptor(rnnDesc)
    checkstatus(ccall((:cudnnDestroyRNNDescriptor,libcudnn),cudnnStatus_t,(cudnnRNNDescriptor_t,),rnnDesc))
end

function cudnnSetRNNDescriptor(rnnDesc,hiddenSize,numLayers,dropoutDesc,inputMode,direction,mode,dataType)
    checkstatus(ccall((:cudnnSetRNNDescriptor,libcudnn),cudnnStatus_t,(cudnnRNNDescriptor_t,Cint,Cint,cudnnDropoutDescriptor_t,cudnnRNNInputMode_t,cudnnDirectionMode_t,cudnnRNNMode_t,cudnnDataType_t),rnnDesc,hiddenSize,numLayers,dropoutDesc,inputMode,direction,mode,dataType))
end

function cudnnGetRNNWorkspaceSize(handle,rnnDesc,seqLength,xDesc,sizeInBytes)
    checkstatus(ccall((:cudnnGetRNNWorkspaceSize,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,Cint,Ptr{cudnnTensorDescriptor_t},Ptr{Cint}),handle,rnnDesc,seqLength,xDesc,sizeInBytes))
end

function cudnnGetRNNTrainingReserveSize(handle,rnnDesc,seqLength,xDesc,sizeInBytes)
    checkstatus(ccall((:cudnnGetRNNTrainingReserveSize,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,Cint,Ptr{cudnnTensorDescriptor_t},Ptr{Cint}),handle,rnnDesc,seqLength,xDesc,sizeInBytes))
end

function cudnnGetRNNParamsSize(handle,rnnDesc,xDesc,sizeInBytes,dataType)
    checkstatus(ccall((:cudnnGetRNNParamsSize,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,cudnnTensorDescriptor_t,Ptr{Cint},cudnnDataType_t),handle,rnnDesc,xDesc,sizeInBytes,dataType))
end

function cudnnGetRNNLinLayerMatrixParams(handle,rnnDesc,layer,xDesc,wDesc,w,linLayerID,linLayerMatDesc,linLayerMat)
    checkstatus(ccall((:cudnnGetRNNLinLayerMatrixParams,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,Cint,cudnnTensorDescriptor_t,cudnnFilterDescriptor_t,Ptr{Void},Cint,cudnnFilterDescriptor_t,Ptr{Ptr{Void}}),handle,rnnDesc,layer,xDesc,wDesc,w,linLayerID,linLayerMatDesc,linLayerMat))
end

function cudnnGetRNNLinLayerBiasParams(handle,rnnDesc,layer,xDesc,wDesc,w,linLayerID,linLayerBiasDesc,linLayerBias)
    checkstatus(ccall((:cudnnGetRNNLinLayerBiasParams,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,Cint,cudnnTensorDescriptor_t,cudnnFilterDescriptor_t,Ptr{Void},Cint,cudnnFilterDescriptor_t,Ptr{Ptr{Void}}),handle,rnnDesc,layer,xDesc,wDesc,w,linLayerID,linLayerBiasDesc,linLayerBias))
end

function cudnnRNNForwardInference(handle,rnnDesc,seqLength,xDesc,x,hxDesc,hx,cxDesc,cx,wDesc,w,yDesc,y,hyDesc,hy,cyDesc,cy,workspace,workSpaceSizeInBytes)
    checkstatus(ccall((:cudnnRNNForwardInference,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,Cint,Ptr{cudnnTensorDescriptor_t},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnFilterDescriptor_t,Ptr{Void},Ptr{cudnnTensorDescriptor_t},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Cint),handle,rnnDesc,seqLength,xDesc,x,hxDesc,hx,cxDesc,cx,wDesc,w,yDesc,y,hyDesc,hy,cyDesc,cy,workspace,workSpaceSizeInBytes))
end

function cudnnRNNForwardTraining(handle,rnnDesc,seqLength,xDesc,x,hxDesc,hx,cxDesc,cx,wDesc,w,yDesc,y,hyDesc,hy,cyDesc,cy,workspace,workSpaceSizeInBytes,reserveSpace,reserveSpaceSizeInBytes)
    checkstatus(ccall((:cudnnRNNForwardTraining,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,Cint,Ptr{cudnnTensorDescriptor_t},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnFilterDescriptor_t,Ptr{Void},Ptr{cudnnTensorDescriptor_t},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Cint,Ptr{Void},Cint),handle,rnnDesc,seqLength,xDesc,x,hxDesc,hx,cxDesc,cx,wDesc,w,yDesc,y,hyDesc,hy,cyDesc,cy,workspace,workSpaceSizeInBytes,reserveSpace,reserveSpaceSizeInBytes))
end

function cudnnRNNBackwardData(handle,rnnDesc,seqLength,yDesc,y,dyDesc,dy,dhyDesc,dhy,dcyDesc,dcy,wDesc,w,hxDesc,hx,cxDesc,cx,dxDesc,dx,dhxDesc,dhx,dcxDesc,dcx,workspace,workSpaceSizeInBytes,reserveSpace,reserveSpaceSizeInBytes)
    checkstatus(ccall((:cudnnRNNBackwardData,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,Cint,Ptr{cudnnTensorDescriptor_t},Ptr{Void},Ptr{cudnnTensorDescriptor_t},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnFilterDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{cudnnTensorDescriptor_t},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},Cint,Ptr{Void},Cint),handle,rnnDesc,seqLength,yDesc,y,dyDesc,dy,dhyDesc,dhy,dcyDesc,dcy,wDesc,w,hxDesc,hx,cxDesc,cx,dxDesc,dx,dhxDesc,dhx,dcxDesc,dcx,workspace,workSpaceSizeInBytes,reserveSpace,reserveSpaceSizeInBytes))
end

function cudnnRNNBackwardWeights(handle,rnnDesc,seqLength,xDesc,x,hxDesc,hx,yDesc,y,workspace,workSpaceSizeInBytes,dwDesc,dw,reserveSpace,reserveSpaceSizeInBytes)
    checkstatus(ccall((:cudnnRNNBackwardWeights,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnRNNDescriptor_t,Cint,Ptr{cudnnTensorDescriptor_t},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{cudnnTensorDescriptor_t},Ptr{Void},Ptr{Void},Cint,cudnnFilterDescriptor_t,Ptr{Void},Ptr{Void},Cint),handle,rnnDesc,seqLength,xDesc,x,hxDesc,hx,yDesc,y,workspace,workSpaceSizeInBytes,dwDesc,dw,reserveSpace,reserveSpaceSizeInBytes))
end

function cudnnSetFilter4dDescriptor_v3(filterDesc,dataType,k,c,h,w)
    checkstatus(ccall((:cudnnSetFilter4dDescriptor_v3,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,cudnnDataType_t,Cint,Cint,Cint,Cint),filterDesc,dataType,k,c,h,w))
end

function cudnnSetFilter4dDescriptor_v4(filterDesc,dataType,format,k,c,h,w)
    checkstatus(ccall((:cudnnSetFilter4dDescriptor_v4,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,cudnnDataType_t,cudnnTensorFormat_t,Cint,Cint,Cint,Cint),filterDesc,dataType,format,k,c,h,w))
end

function cudnnGetFilter4dDescriptor_v3(filterDesc,dataType,k,c,h,w)
    checkstatus(ccall((:cudnnGetFilter4dDescriptor_v3,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,Ptr{cudnnDataType_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),filterDesc,dataType,k,c,h,w))
end

function cudnnGetFilter4dDescriptor_v4(filterDesc,dataType,format,k,c,h,w)
    checkstatus(ccall((:cudnnGetFilter4dDescriptor_v4,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,Ptr{cudnnDataType_t},Ptr{cudnnTensorFormat_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),filterDesc,dataType,format,k,c,h,w))
end

function cudnnSetFilterNdDescriptor_v3(filterDesc,dataType,nbDims,filterDimA)
    checkstatus(ccall((:cudnnSetFilterNdDescriptor_v3,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,cudnnDataType_t,Cint,Ptr{Cint}),filterDesc,dataType,nbDims,filterDimA))
end

function cudnnSetFilterNdDescriptor_v4(filterDesc,dataType,format,nbDims,filterDimA)
    checkstatus(ccall((:cudnnSetFilterNdDescriptor_v4,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,cudnnDataType_t,cudnnTensorFormat_t,Cint,Ptr{Cint}),filterDesc,dataType,format,nbDims,filterDimA))
end

function cudnnGetFilterNdDescriptor_v3(filterDesc,nbDimsRequested,dataType,nbDims,filterDimA)
    checkstatus(ccall((:cudnnGetFilterNdDescriptor_v3,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,Cint,Ptr{cudnnDataType_t},Ptr{Cint},Ptr{Cint}),filterDesc,nbDimsRequested,dataType,nbDims,filterDimA))
end

function cudnnGetFilterNdDescriptor_v4(filterDesc,nbDimsRequested,dataType,format,nbDims,filterDimA)
    checkstatus(ccall((:cudnnGetFilterNdDescriptor_v4,libcudnn),cudnnStatus_t,(cudnnFilterDescriptor_t,Cint,Ptr{cudnnDataType_t},Ptr{cudnnTensorFormat_t},Ptr{Cint},Ptr{Cint}),filterDesc,nbDimsRequested,dataType,format,nbDims,filterDimA))
end

function cudnnSetPooling2dDescriptor_v3(poolingDesc,mode,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride)
    checkstatus(ccall((:cudnnSetPooling2dDescriptor_v3,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,cudnnPoolingMode_t,Cint,Cint,Cint,Cint,Cint,Cint),poolingDesc,mode,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride))
end

function cudnnSetPooling2dDescriptor_v4(poolingDesc,mode,maxpoolingNanOpt,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride)
    checkstatus(ccall((:cudnnSetPooling2dDescriptor_v4,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,cudnnPoolingMode_t,cudnnNanPropagation_t,Cint,Cint,Cint,Cint,Cint,Cint),poolingDesc,mode,maxpoolingNanOpt,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride))
end

function cudnnGetPooling2dDescriptor_v3(poolingDesc,mode,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride)
    checkstatus(ccall((:cudnnGetPooling2dDescriptor_v3,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,Ptr{cudnnPoolingMode_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,mode,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride))
end

function cudnnGetPooling2dDescriptor_v4(poolingDesc,mode,maxpoolingNanOpt,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride)
    checkstatus(ccall((:cudnnGetPooling2dDescriptor_v4,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,Ptr{cudnnPoolingMode_t},Ptr{cudnnNanPropagation_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,mode,maxpoolingNanOpt,windowHeight,windowWidth,verticalPadding,horizontalPadding,verticalStride,horizontalStride))
end

function cudnnSetPoolingNdDescriptor_v3(poolingDesc,mode,nbDims,windowDimA,paddingA,strideA)
    checkstatus(ccall((:cudnnSetPoolingNdDescriptor_v3,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,cudnnPoolingMode_t,Cint,Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,mode,nbDims,windowDimA,paddingA,strideA))
end

function cudnnSetPoolingNdDescriptor_v4(poolingDesc,mode,maxpoolingNanOpt,nbDims,windowDimA,paddingA,strideA)
    checkstatus(ccall((:cudnnSetPoolingNdDescriptor_v4,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,cudnnPoolingMode_t,cudnnNanPropagation_t,Cint,Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,mode,maxpoolingNanOpt,nbDims,windowDimA,paddingA,strideA))
end

function cudnnGetPoolingNdDescriptor_v3(poolingDesc,nbDimsRequested,mode,nbDims,windowDimA,paddingA,strideA)
    checkstatus(ccall((:cudnnGetPoolingNdDescriptor_v3,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,Cint,Ptr{cudnnPoolingMode_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,nbDimsRequested,mode,nbDims,windowDimA,paddingA,strideA))
end

function cudnnGetPoolingNdDescriptor_v4(poolingDesc,nbDimsRequested,mode,maxpoolingNanOpt,nbDims,windowDimA,paddingA,strideA)
    checkstatus(ccall((:cudnnGetPoolingNdDescriptor_v4,libcudnn),cudnnStatus_t,(cudnnPoolingDescriptor_t,Cint,Ptr{cudnnPoolingMode_t},Ptr{cudnnNanPropagation_t},Ptr{Cint},Ptr{Cint},Ptr{Cint},Ptr{Cint}),poolingDesc,nbDimsRequested,mode,maxpoolingNanOpt,nbDims,windowDimA,paddingA,strideA))
end

function cudnnActivationForward_v3(handle,mode,alpha,xDesc,x,beta,yDesc,y)
    checkstatus(ccall((:cudnnActivationForward_v3,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnActivationMode_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,mode,alpha,xDesc,x,beta,yDesc,y))
end

function cudnnActivationForward_v4(handle,activationDesc,alpha,xDesc,x,beta,yDesc,y)
    checkstatus(ccall((:cudnnActivationForward_v4,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnActivationDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,activationDesc,alpha,xDesc,x,beta,yDesc,y))
end

function cudnnActivationBackward_v3(handle,mode,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx)
    checkstatus(ccall((:cudnnActivationBackward_v3,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnActivationMode_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,mode,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx))
end

function cudnnActivationBackward_v4(handle,activationDesc,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx)
    checkstatus(ccall((:cudnnActivationBackward_v4,libcudnn),cudnnStatus_t,(cudnnHandle_t,cudnnActivationDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void},Ptr{Void},cudnnTensorDescriptor_t,Ptr{Void}),handle,activationDesc,alpha,yDesc,y,dyDesc,dy,xDesc,x,beta,dxDesc,dx))
end


text:
module LennardJones

include("./utils.jl")
include("./bases.jl")
include("./wca.jl")

end

ext: jl


text:
include("functions_endoL.jl")
include("NewtonRoot.jl")

## Test Code ##

# Check policy functions for c and a
r0 = 0.015;
AA0, cpol0 = AiyagariEGM(r0);
abar0 = zeros(length(cpol0));
cbar1 = EGM(AA0,cpol0,abar0)
cpol_EGM, apol_EGM = SolveEGM(AA0,cpol0,abar0);

plot(AA0.aGrid, AA0.aGrid, label = "", color = :black, linestyle = :dash)
plot!(AA0.aGrid, apol_EGM[1:10,:], label = "ϵ = $(round(AA0.ϵGrid[1], digits = 3))")
plot!(AA0.aGrid, apol_EGM[41:50,:], label = "ϵ = $(round(AA0.ϵGrid[5], digits = 3))")

# Check stationary distribution
λ_init_test = zeros(AA0.na*AA0.nϵ) .+ 1/(AA0.na*AA0.nϵ);
Qmat_test = zeros(AA0.na*AA0.nϵ,AA0.na*AA0.nϵ);
Qmat_test = MakeTransMatEGM(AA0,apol_EGM,Qmat_test);
λ_test = StationaryDistributionEGM(Qmat_test,λ_init_test)

sum(λ_test) # all elements sum to one
dot(λ_test, AA0.aGridl) # aggregate asset
dot(λ_test, apol_EGM)
dot(λ_test, repeat(AA0.ϵGrid, inner = AA0.na)) # aggregate labor

# Check equilibrium of the model
equilibriumEGM(AA0,cpol0,abar0) # Still get negative r at equilibrium

# Check aggregate demand and aggregate supply of capital
r_vals = LinRange(0.0, 1/0.98-1,100)
Ar_vals = zeros(length(r_vals))
Nbar_vals = zeros(length(r_vals))
K_vals = zeros(length(r_vals))

for i = 1:length(r_vals)
    r0 = r_vals[i];
    AA0, cpol0 = AiyagariEGM(r0);
    abar0 = zeros(length(cpol0));
    cpol_EGM, apol_EGM = SolveEGM(AA0,cpol0,abar0);

    λ_init_test = zeros(AA0.na*AA0.nϵ) .+ 1/(AA0.na*AA0.nϵ);
    Qmat_test = zeros(AA0.na*AA0.nϵ,AA0.na*AA0.nϵ);
    Qmat_test = MakeTransMatEGM(AA0,apol_EGM,Qmat_test);
    λ_test = StationaryDistributionEGM(Qmat_test,λ_init_test)

    Ar_vals[i] = dot(λ_test, AA0.aGridl)
    Nbar_vals[i] = dot(λ_test, repeat(AA0.ϵGrid, inner = AA0.na));
    K_vals[i] = ((r0+AA0.params.δ)/(AA0.params.θ*Nbar_vals[i]^(1-AA0.params.θ)))^(1/(AA0.params.θ-1));
end

plot(S_vals,r_vals)
plot!(K_vals,r_vals)

# Incorporate elastic labor supply
using ForwardDiff
U(c,l) = (c^(1-0.7)/(1-0.7))-((1-l)^(1+0.3)/(1+0.3));
Uc0(c,l) = ForwardDiff.derivative(c -> U(c,l),c);
Uc = l -> Uc0(c,l)
Ul0(c,l) = ForwardDiff.derivative(l -> U(c,l), l);
Ul = l -> Ul0(c,l)

c = 0.38072170674937644
Uc(0.8)
Ul(0.9)
NewtonRoot(l -> Ul(l)/Uc(l) - 0.5, 1)
Solve_l(c) = NewtonRoot(l -> Ul(l)/Uc(l) - w*ϵ, 1)

#F(c,l) = c^2*l^2

#Fc(c,l) = ForwardDiff.derivative(c -> F(c,l), c)
#Fc_new = l -> Fc(c,l) # Fc with only function of l
#Fl(c,l) = ForwardDiff.derivative(l -> F(c,l), l)
#Fl_new = l -> Fl(c,l)

#c = 1.5
#NewtonRoot(l -> Fl_new(l)/Fc_new(l) - 2.0, 1)

ϵ_test = 3.08
c_test = 3.6

l_test = (2.33*c_test)/(1.17*ϵ_test)


text:
using Genie.Router
using HeresController

route("/") do
  serve_static_file("welcome.html")
end

route("/heres", HeresController.index, named=:get_heres)
route("/heres", HeresController.create, method=POST, named=:create_here)
route("/heres/:id", HeresController.show, named=:get_here)
route("/heres/:id", HeresController.delete, method=DELETE, named=:delete_here)

max_stars_repo_name: huaxk/rlservice

max_issues_repo_name: huaxk/rlservice

max_forks_repo_name: huaxk/rlservice


text:
using PiecewiseLinearOpt
using Base.Test

using JuMP, Cbc
const solver = CbcSolver()

methods_1D = (:CC,:MC,:Logarithmic,:ZigZag,:ZigZagInteger,:SOS2,:GeneralizedCelaya,:SymmetricCelaya,:Incremental)
methods_2D = (:CC,:MC,:Logarithmic,:ZigZag,:ZigZagInteger,:SOS2,:GeneralizedCelaya,:SymmetricCelaya)

using HDF5

const instance_data_1D = joinpath(dirname(@__FILE__),"1D-pwl-instances.h5")
const instance_data_2D = joinpath(dirname(@__FILE__),"2D-pwl-instances.h5")

for instance in ["10104_1_concave_1"]
    objs = Dict()

    demand = h5read(instance_data_1D, string(instance,"/demand"))
    supply = h5read(instance_data_1D, string(instance,"/supply"))
    numdem = size(demand, 1)
    numsup = size(supply, 1)

    d  = h5read(instance_data_1D, string(instance,"/d"))
    fd = h5read(instance_data_1D, string(instance,"/fd"))
    K = size(d, 2)

    for method in methods_1D
        model = Model(solver=solver)
        @variable(model, x[1:numsup,1:numdem] ≥ 0)
        for j in 1:numdem
            # demand constraint
            @constraint(model, sum(x[i,j] for i in 1:numsup) == demand[j])
        end
        for i in 1:numsup
            # supply constraint
            @constraint(model, sum(x[i,j] for j in 1:numdem) == supply[i])
        end

        idx = 1
        obj = AffExpr()
        for i in 1:numsup, j in 1:numdem
            z = piecewiselinear(model, x[i,j], d[idx,:], fd[idx,:], method=method)
            obj += z
        end
        @objective(model, Min, obj)

        stat = solve(model)
        objs[method] = getobjectivevalue(model)
    end
    vals = collect(values(objs))
    for i in 2:length(vals)
        @test isapprox(vals[i-1], vals[i], rtol=1e-4)
    end
end

# for numpieces in [4,8,16,32], variety in 1:5, objective in 1:20
for numpieces in [4], variety in 1:5, objective in 1:20
    instance = string(numpieces,"_",variety,"_",objective)
    objs = Dict()

    demand = h5read(instance_data_2D, string(instance,"/demand"))
    supply = h5read(instance_data_2D, string(instance,"/supply"))
    numdem = size(demand, 1)
    numsup = size(supply, 1)

    d  = h5read(instance_data_2D, string(instance,"/d"))
    fd = h5read(instance_data_2D, string(instance,"/fd"))
    K = size(d, 2)

    for method in methods_2D
        model = Model(solver=solver)
        @variable(model, x[1:numsup,1:numdem] ≥ 0)
        @variable(model, y[1:numsup,1:numdem] ≥ 0)

        for j in 1:numdem
            # demand constraint
            @constraint(model, sum(x[i,j] for i in 1:numsup) == demand[j])
            @constraint(model, sum(y[i,j] for i in 1:numsup) == demand[j])
        end
        for i in 1:numsup
            # supply constraint
            @constraint(model, sum(x[i,j] for j in 1:numdem) == supply[i])
            @constraint(model, sum(y[i,j] for j in 1:numdem) == supply[i])
        end

        for i in 1:numdem, j in 1:numsup
            @constraint(model, x[i,j] + y[i,j] ≤ 1.5d[end])
        end

        idx = 1
        obj = AffExpr()
        for i in 1:numsup, j in 1:numdem
            dˣ =  d[idx,:]
            fˣ = reshape(fd[idx,:], length(dˣ), length(dˣ))
            ˣtoⁱ = Dict(dˣ[i] => i for i in 1:length(dˣ))
            fp = (pˣ,pʸ) -> fˣ[ˣtoⁱ[pˣ],ˣtoⁱ[pʸ]]
            z = piecewiselinear(model, x[i,j], y[i,j], BivariatePWLFunction(dˣ, dˣ, fp, pattern=:UnionJack), method=method)
            obj += z
        end
        @objective(model, Min, obj)

        stat = solve(model)
        objs[method] = getobjectivevalue(model)
    end
    vals = collect(values(objs))
    for i in 2:length(vals)
        @test isapprox(vals[i-1], vals[i], rtol=1e-4)
    end
end

max_stars_repo_name: YP-Ye/PiecewiseLinearOpt

max_issues_repo_name: YP-Ye/PiecewiseLinearOpt

max_forks_repo_name: YP-Ye/PiecewiseLinearOpt


text:
using Distributions: kldivergence
using KLDivergences
using Distributions
using LinearAlgebra
using Random
using StatsBase
using Test

@testset "KLDivergences.jl" begin
    include("univariate.jl")
    include("multivariate.jl")

    @testset "Generic Methods" begin
        p = Exponential(2.0)
        q = Exponential(5.0)
        @test symmetricKL(p, q) == symmetricKL(q, p)
    end

end


text:
module ProblemParser

export Apply
export Blocks
export Convert
export FirstRest
export LineMappings
export Lines
export Map
export Extract
export Mappings
export Noop
export Rectangular
export Split

const universal_newlines = r"\r?\n|\v|\f|\u1c|\u1d|\u1e|\u85|\u2028|\u2029"
const doubled_newlines = Regex("(?:" * universal_newlines.pattern * "){2}")

abstract type GrammarElement end

Base.@kwdef struct Noop <: GrammarElement
    inspect::Bool = false
end

struct Split <: GrammarElement
    splitter
    per_element::Union{GrammarElement,Nothing}
    keepempty::Bool
    limit::Int
end
Split(splitter=isspace; keepempty=false, limit=0) = Split(splitter, nothing, keepempty, limit)
Split(per_element::GrammarElement; keepempty=false, limit=0) = Split(isspace, per_element, keepempty, limit)
Split(splitter, per_element::GrammarElement; keepempty=false, limit=0) = Split(splitter, per_element, keepempty, limit)

Lines(per_line::Union{GrammarElement,Nothing}=nothing; keepempty=false) = Split(universal_newlines, per_line, keepempty, 0)

Base.@kwdef struct Convert <: GrammarElement
    target_type::Type = Int
end

Base.@kwdef struct Rectangular <: GrammarElement
    per_entry::Union{GrammarElement,Nothing} = nothing
end

Blocks(per_block=nothing; keepempty=false) = Split(doubled_newlines, per_block, keepempty, 0)

struct FirstRest <: GrammarElement
    splitter
    for_first::Union{GrammarElement,Nothing}
    per_rest::Union{GrammarElement,Nothing}
end
FirstRest() = FirstRest(Lines(), nothing, nothing)
FirstRest(split_on, per_rest=nothing) = FirstRest(split_on, nothing, per_rest)

struct Apply <: GrammarElement
    func
end

struct Map <: GrammarElement
    func
end

struct Extract <: GrammarElement
    regex::Regex
    after::Union{GrammarElement,Nothing}
end
Extract() = Extract(r"\d+", Convert())
Extract(regex::Regex) = Extract(regex, nothing)

struct _Mappings <: GrammarElement
    splitter::Union{GrammarElement,Nothing}
    first_rest::FirstRest
end
Mappings(splitter, args...) = _Mappings(splitter, FirstRest(args...))
LineMappings(args...) = _Mappings(Lines(), FirstRest(args...))

Base.parse(::Nothing, anything) = anything

function Base.parse(noop::Noop, anything)
    noop.inspect && println(anything)
    anything
end

Base.parse(operation::GrammarElement, list) = map(Base.Fix1(Base.parse, operation), list)

Base.parse(convert::Convert, text::Union{AbstractString, Char}) = parse(convert.target_type, text)

function Base.parse(split::Split, text::AbstractString)
    parts = Base.split(text, split.splitter; keepempty=split.keepempty, limit=split.limit)
    parse(split.per_element, parts)
end

function Base.parse(rectangular::Rectangular, text::AbstractString)
    if rectangular.per_entry isa Split
        splitfirst = parse(rectangular.per_entry, text)
        permutedims(reduce(hcat, splitfirst))
    else
        rows = parse(Lines(), text)
        chars_as_columns = map(collect, rows)
        transposed = reduce(hcat, chars_as_columns)
        parse(rectangular.per_entry, permutedims(transposed))
    end
end

function Base.parse(firstrest::FirstRest, text::AbstractString)
    first, rest = split(text, firstrest.splitter.splitter; keepempty=true, limit=2)
    parse(firstrest.for_first, first), parse(firstrest.per_rest, rest)
end

Base.parse(apply::Apply, thing) = apply.func(thing)
Base.parse(map::Map, list::AbstractArray) = Base.map(map.func, list)
Base.parse(map::Map, text::AbstractString) = Base.map(map.func, collect(text))

Base.parse(extract::Extract, text::AbstractString) = parse(extract.after, match(extract.regex, text).match)

function Base.parse(mappings::_Mappings, text::AbstractString)
    kv_pairs = parse(mappings.splitter, text)
    parse(mappings.first_rest, kv_pairs) |> Dict
end

end # module

max_stars_repo_name: CmdQ/AoC

max_issues_repo_name: CmdQ/AoC

max_forks_repo_name: CmdQ/AoC


text:
using Printf
S = subtypes(Signed)
U = subtypes(Unsigned)

println("Integer limits:")
for (s, u) in zip(S, U)
    @printf("%8s: [%s, %s]\n", s, typemin(s), typemax(s))
    @printf("%8s: [%s, %s]\n", u, typemin(u), typemax(u))
end

max_stars_repo_name: ethansaxenian/RosettaDecode

max_issues_repo_name: ethansaxenian/RosettaDecode

max_forks_repo_name: ethansaxenian/RosettaDecode


text:
# spatial directions -> spatial order

function dir2ord(xi::T, xj::T, xk::T, yi::T, yj::T, yk::T, zi::T, zj::T, zk::T) where {T}
    # Normalize column vectors to get unit vectors along each ijk-axis
    # normalize i axis
    val = sqrt(xi*xi + yi*yi + zi*zi)
    if val == 0.0
        error("Invalid rotation directions.")
    end
    xi /= val
    yi /= val
    zi /= val

    # normalize j axis
    val = sqrt(xj*xj + yj*yj + zj*zj)
    if val == 0.0
        error("Invalid rotation directions.")
    end
    xj /= val
    yj /= val
    zj /= val

    # orthogonalize j axis to i axis, if needed
    val = xi*xj + yi*yj + zi* zj  # dot product between i and j
    if abs(val) > .0001
        xj -= val*xi
        yj -= val*yi
        zj -= val*zi

        val = sqrt(xj*xj + yj*yj + zj*zj)  # must renormalize
        if val == 0.0
            error("The first and second dimensions cannot be parallel.")
        end
        xj /= val
        yj /= val
        zj /= val
    end

    # normalize k axis; if it is zero, make it the cross product i x j
    val = sqrt(xk*xk + yk*yk + zk*zk)
    if val == 0.0
        xk = yi*zj-zi*yj
        yk = zi*xj-zj*xi
        zk = xi*yj-yi*xj
    else
        xk = xk/val
        yk = yk/val
        zk = zk/val
    end

    # orthogonalize k to i
    val = xi*xk + yi*yk + zi*zk  # dot product between i and k
    if abs(val) > 0.0001
        xk -= val*xi
        yk -= val*yi
        zk -= val*zi

        # must renormalize
        val = sqrt(xk*xk + yk*yk + zk*zk)
        if val == 0.0
            return 0  # I think this is suppose to be an error output
        end
        xk /= val
        yk /= val
        zk /= val
    end

    # orthogonalize k to j */
    val = xj*xk + yj*yk + zj*zk  # dot product between j and k
   if abs(val) > 0.0001
       xk -= val*xj
       yk -= val*yj
       zk -= val*zj

       val = sqrt(xk*xk + yk*yk + zk*zk)
       if val == 0.0
           return 0  # bad
       end
       xk /= val
       yk /= val
       zk /= val
   end

    Q = T[[xi xj xk]
          [yi yj yk]
          [zi zj zk]]

    # at this point Q is the rotation matrix from the (i,j,k) to (x,y,z) axes
    detQ = det(Q)
    # if( detQ == 0.0 ) return ; /* shouldn't happen unless user is a DUFIS */

    # Build and test all possible +1/-1 coordinate permutation matrices P;
    # then find the P such that the rotation matrix M=PQ is closest to the
    # identity, in the sense of M having the smallest total rotation angle.

    # Despite the formidable looking 6 nested loops, there are
    # only 3*3*3*2*2*2 = 216 passes, which will run very quickly.
    vbest = T(-666)
    ibest = pbest=qbest=rbest= 1.0
    jbest = 2.0
    kbest = 3.0
    @inbounds for i in 1:3       # i = column number to use for row #1
        for j in 1:3             # j = column number to use for row #2
            if i == j
                continue
            end
            for k in 1:3     # k = column number to use for row #3
                if i == k || j ==k
                    continue
                end
                P = fill(0.0, 3, 3)
                for p in (-1, 1)           # p,q,r are -1 or +1
                    for q in (-1, 1)       # and go into rows 1,2,3
                        for r in (-1, 1)
                            P[1,i] = p
                            P[2,j] = q
                            P[3,k] = r
                            detP = det(P)  # sign of permutation
                            if detP * detQ < 0.0  # doesn't match sign of Q
                                continue
                            end
                            M = P * Q
                            # angle of M rotation = 2.0 * acos(0.5 * sqrt(1.0 + trace(M)))
                            # we want largest trace(M) == smallest angle == M nearest to I
                            val = M[1,1] + M[2,2] + M[3,3]
                            if val > vbest
                                vbest = val
                                ibest = i
                                jbest = j
                                kbest = k
                                pbest = p
                                qbest = q
                                rbest = r
                            end
                        end
                    end
                end
            end
        end
    end
    # At this point ibest is 1 or 2 or 3; pbest is -1 or +1; etc.

    # The matrix P that corresponds is the best permutation approximation
    # to Q-inverse; that is, P (approximately) takes (x,y,z) coordinates
    # to the (i,j,k) axes.

    # For example, the first row of P (which contains pbest in column ibest)
    # determines the way the i axis points relative to the anatomical
    # (x,y,z) axes.  If ibest is 2, then the i axis is along the y axis,
    # which is direction P2A (if pbest > 0) or A2P (if pbest < 0).

    # So, using ibest and pbest, we can assign the output code for
    # the i axis.  Mutatis mutandis for the j and k axes, of course.

    return (encoding_name(ibest*pbest), encoding_name(jbest*qbest), encoding_name(kbest*rbest))
end

function dir2ord(sd::NTuple{3,NTuple{3,Any}})
    @inbounds dir2ord(
        ustrip(sd[1][1]), ustrip(sd[1][2]), ustrip(sd[1][3]),
        ustrip(sd[2][1]), ustrip(sd[2][2]), ustrip(sd[2][3]),
        ustrip(sd[3][1]), ustrip(sd[3][2]), ustrip(sd[3][3])
    )
end

@inline function dir2ord(xi, xj, xk, yi, yj, yk, zi, zj, zk)
    return dir2ord(promote(xi, xj, xk, yi, yj, yk, zi, zj, zk)...)
end


text:
module OCCA
include("occapaths.jl");
include("occabuiltwith.jl");

#Flags for which threading libraries to build into OCCA.
USE_OPENMP   = OCCA_USE_OPENMP;
USE_PTHREADS = OCCA_USE_PTHREADS;
USE_CUDA     = OCCA_USE_CUDA;
USE_OPENCL   = OCCA_USE_OPENCL;

thisfile= @__FILE__();
thisdir = dirname(thisfile);
tmpdir  = pwd();

cd(thisdir);
cd("../deps");

#Point to OCCA shared library.
ENV["OCCA_DIR"]=pwd() * "/OCCA2"

cd(tmpdir);

#---[ Types ]-----------------
type Device
    cdevice::Ptr{Void}
end

function Device(infos::String)
    cdevice = ccall((:occaGetDevice, libocca),
                    Ptr{Void},
                    (Ptr{Uint8},),
                    bytestring(infos));
   return Device(cdevice);
end

function Device(infos::String)
    cdevice = ccall((:occaGetDevice, libocca),
                    Ptr{Void},
                    (Ptr{Uint8},),
                    bytestring(infos));
   return Device(cdevice);
end

function Device(;mode        = "",
                threadCount = -1,
                schedule    = "",
                pinnedCores = Int32[],
                deviceID    = -1,
                platformID  = -1)

    infos::String = "";

    if mode != ""
        infos *= string("mode = ",  mode)
    end

    if 0 <= threadCount
        infos *= string(", threadCount = ", threadCount)
    end

    if schedule != ""
        infos *= string(", schedule = ", schedule)
    end

    if 0 < length(pinnedCores)
        infos *= string(", pinnedCores = [", pinnedCores[1])

        for core in pinnedCores[2:end]
            infos *= string(", ", core)
        end

        infos *= "]"
    end

    if 0 <= deviceID
        infos *= string(", deviceID = ", deviceID)
    end

    if 0 <= platformID
        infos *= string(", platformID = ", platformID)
    end

    return Device(infos)
end

type Stream
    cstream::Ptr{Void}
end

type Kernel
    ckernel::Ptr{Void}
end

type KernelInfo
    ckernelinfo::Ptr{Void}
end

function KernelInfo()
    return KernelInfo(ccall((:occaCreateKernelInfo, libocca),Ptr{Void},()));
end

type Memory
    cmemory::Ptr{Void}
    ctypes
end

#---[ Device ]----------------
function free(d::Device)
    ccall((:occaDeviceFree, libocca),
          Void,
          (Ptr{Void},),
          d.cdevice)
end

function mode(d::Device)
    cmode = ccall((:occaDeviceMode, libocca),
                  Ptr{Uint8},
                  (Ptr{Void},), d.cDevice)

    return bytestring(cmode)
end

function set!(d::Device;compiler="",flags="")

    if length(compiler)>0
        ccall((:occaDeviceSetCompiler, libocca),
              Void,
              (Ptr{Void}, Ptr{Uint8},),
              d.cdevice, bytestring(compiler));
    end

    if length(flags)>0
        ccall((:occaDeviceSetCompilerFlags, libocca),
              Void,
              (Ptr{Void}, Ptr{Uint8},),
              d.cDevice, bytestring(flags));
    end

end

function buildkernel(d::Device,filename::String,functionName::String;binary=false)
    if binary
      return Kernel(ccall((:occaBuildKernelFromBinary, libocca),
                    Ptr{Void},
                    (Ptr{Void}, Ptr{Uint8}, Ptr{Uint8},),
                    d.cDevice,
                    bytestring(filename),
                    bytestring(functionName)));
    else
         return Kernel(ccall((:occaBuildKernelFromSource, libocca),
                        Ptr{Void},
                        (Ptr{Void}, Ptr{Uint8}, Ptr{Uint8}, Ptr{Void},),
                        d.cdevice,
                        bytestring(filename),
                        bytestring(functionName),
                        C_NULL));
    end
end

function buildkernel(d::Device,filename::String,functionName::String,info::KernelInfo)
       return  Kernel(ccall((:occaBuildKernelFromSource, libocca),
                        Ptr{Void},
                        (Ptr{Void}, Ptr{Uint8}, Ptr{Uint8}, Ptr{Void},),
                        d.cdevice,
                        bytestring(filename),
                        bytestring(functionName),
                        info.ckernelinfo));
end

function malloc(d::Device, source::Array)
    ctypes = typeof(source[1])
    bytes  = length(source) * sizeof(ctypes)

    convert(Uint, bytes)

    cmemory = ccall((:occaDeviceMalloc, libocca),
                    Ptr{Void},
                    (Ptr{Void}, Uint, Ptr{Void},),
                    d.cdevice, bytes, pointer(source))

    return Memory(cmemory, ctypes)
end

function malloc(d::Device, t::Type, nentries)
    bytes  =  sizeof(t)*nentries;
    convert(Uint, bytes)
    cmemory = ccall((:occaDeviceMalloc, libocca),
                    Ptr{Void},
                    (Ptr{Void}, Uint, Ptr{Void},),
                    d.cdevice, bytes, C_NULL);
    return Memory(cmemory, t)
end

function flush(d::Device)
    ccall((:occaDeviceFlush, libocca),
          Void,
          (Ptr{Void},), d.cDevice)
end

function finish(d::Device)
    ccall((:occaDeviceFinish, libocca),
          Void,
          (Ptr{Void},), d.cDevice)
end

function createstream(d::Device)
    cstream = ccall((:occaCreateStream, libocca),
                    Ptr{Void},
                    (Ptr{Void},),
                    d.cDevice)

    return Stream(cstream)
end

function getstream(d::Device)
    cstream = ccall((:occaGetStream, libocca),
                    Ptr{Void},
                    (Ptr{Void},),
                    d.cDevice)

    return Stream(cstream)
end

function setstream!(d::Device, s::Stream)
    ccall((:occaSetStream, libocca),
          Void,
          (Ptr{Void}, Ptr{Void},),
          d.cDevice, s.cstream)
end

#---[ Kernel ]----------------
function free(k::Kernel)
    ccall((:occaKernelFree, libocca),
          Void,
          (Ptr{Void},),
          k.cKernel)
end

function mode(k::Kernel)
    cMode = ccall((:occaKernelMode, libocca),
                  Ptr{Uint8},
                  (Ptr{Void},),
                  k.cKernel)

    return bytestring(cMode)
end

function getpreferreddimsize(k::Kernel)
    return ccall((:occaKernelPreferredDimSize, libocca),
                 Int32,
                 (Ptr{Void},),
                 k.cKernel)
end

function setworkingdims!(k::Kernel,
                        dims, items, groups)
    convert(Int32, dims)

    items_  = ones(Uint, 3)
    groups_ = ones(Uint, 3)

    for i = 1:dims
        items_[i]  = items[i]
        groups_[i] = groups[i]
    end

    ccall((:occaKernelSetAllWorkingDims, libocca),
          Void,
          (Ptr{Void},
           Int32,
           Uint, Uint, Uint,
           Uint, Uint, Uint,),
          k.cKernel,
          dims,
          items_[1] , items_[2] , items_[3],
          groups_[1], groups_[2], groups_[3])
end

argType(arg::Int8)  = ccall((:occaChar, libocca), Ptr{Void}, (Int8,) , arg)
argType(arg::Uint8) = ccall((:occaUChar, libocca), Ptr{Void}, (Uint8,), arg)

argType(arg::Int16)  = ccall((:occaShort, libocca), Ptr{Void}, (Int16,) , arg)
argType(arg::Uint16) = ccall((:occaUShort, libocca), Ptr{Void}, (Uint16,), arg)

argType(arg::Int32)  = ccall((:occaInt, libocca), Ptr{Void}, (Int32,) , arg)
argType(arg::Uint32) = ccall((:occaUInt, libocca), Ptr{Void}, (Uint32,), arg)

argType(arg::Int64)  = ccall((:occaLong, libocca), Ptr{Void}, (Int64,) , arg)
argType(arg::Uint64) = ccall((:occaULong, libocca), Ptr{Void}, (Uint64,), arg)

argType(arg::Float32) = ccall((:occaFloat, libocca), Ptr{Void}, (Float32,) , arg)
argType(arg::Float64) = ccall((:occaDouble, libocca), Ptr{Void}, (Float64,) , arg)

argType(arg::Memory) = arg.cmemory;

function runkernel!(k::Kernel, args...)
    argList = ccall((:occaCreateArgumentList, libocca),
                    Ptr{Void}, ())

    pos = convert(Int32, 0)
    for arg in args
        carg = argType(arg);
        ccall((:occaArgumentListAddArg,libocca),
        Void,
        (Ptr{Void}, Int32, Ptr{Void},),
        argList, pos, carg);
        pos += 1
    end

    ccall((:occaKernelRun_, libocca),
          Void,
          (Ptr{Void}, Ptr{Void},),
          k.ckernel, argList)

    ccall((:occaArgumentListFree, libocca),
          Void,
          (Ptr{Void},),
          argList)
end

function adddefine!(info::KernelInfo, macro_::String, value::String)
    occaValue = ccall((:occaString, libocca),
                      Ptr{Void},
                      (Ptr{Uint8},),
                      bytestring(value))

    ccall((:occaKernelInfoAddDefine, libocca),
          Void,
          (Ptr{Void}, Ptr{Uint8}, Ptr{Void},),
          info.ckernelinfo, bytestring(macro_), occaValue)
end

function addinclude!(info::KernelInfo, includepath::String)
    ccall((:occaKernelInfoAddInclude,libocca),
    Void,
    (Ptr{Void},Ptr{Uint8},),
    info.ckernelinfo,bytestring(includepath));
end

function free(info::KernelInfo)
    ccall((:occaKernelInfoFree, libocca),
          Void,
          (Ptr{Void},),
          info.ckernelinfo)
end

#---[ Memory ]----------------
function free(m::Memory)
    ccall((:occaMemoryFree, libocca),
          Void,
          (Ptr{Void},),
          m.cmemory)
end

function mode(m::Memory)
    cMode = ccall((:occaMemoryMode, libocca),
                  Ptr{Uint8},
                  (Ptr{Void},),
                  m.cmemory)

    return bytestring(cMode)
end

function memcpy!{T}(dest::Memory,src::Array{T})
    destptr=dest.cmemory;
    srcptr = pointer(src);
    ccall((:occaCopyPtrToMem,libocca),Void,(Ptr{Void},Ptr{Void},Uint,Uint,),
    destptr,srcptr,Uint(0),Uint(0));
end

function memcpy!(dest::Memory,src::Memory)
    destptr=dest.cmemory;
    srcptr =src.cmemory;
    ccall((:occaCopyMemToMem, libocca),Void,(Ptr{Void}, Ptr{Void}, Uint, Uint, Uint,),
    destptr, srcptr, Uint(0), Uint(0), Uint(0))
end

function memcpy!{T}(dest::Array{T},src::Memory)
    destptr=pointer(dest);
    srcptr =src.cmemory;
    ccall((:occaCopyMemToPtr,libocca),Void,(Ptr{Void},Ptr{Void},Uint,Uint,),
    destptr,srcptr,Uint(0),Uint(0));
end

function memcpy!{T}(src::Array{T},dest::Array{T})
    for i = 1 : length(src)
        src[i]=dest[i];
    end
end

function swap!(a::Memory, b::Memory)
    tmp       = a.cmemory
    a.cmemory = b.cmemory
    b.cmemory = tmp
end

function rebuildwith!(;pthreads=false,opencl=false,cuda=false,openmp=false)
    f=open(thisdir * "/occabuiltwith.jl","w");

    if openmp
        write(f,"OCCA_USE_OPENMP = true;\n");
    else
        write(f,"OCCA_USE_OPENMP = false;\n");
    end
    if pthreads
        write(f,"OCCA_USE_PTHREADS = true;\n");
    else
        write(f,"OCCA_USE_PTHREADS = false;\n");
    end
    if opencl
        write(f,"OCCA_USE_OPENCL = true;\n");
    else
        write(f,"OCCA_USE_OPENCL = false;\n");
    end
    if openmp
        write(f,"OCCA_USE_CUDA = true;\n");
    else
        write(f,"OCCA_USE_CUDA = false;\n");
    end
    close(f);
    reload("OCCA");

    USE_OPENMP = openmp;
    USE_PTHREADS = pthreads;
    USE_CUDA = cuda;
    USE_OPENCL = opencl;

    Pkg.build("OCCA");
end

function verbosecompile(enabled::Bool)
     ccall((:occaSetVerboseCompilation, libocca),
                    Void,
                    (Int32,),
                    Int32(enabled ? 1 : 0));
end

end


text:
using Test, Mosek

@testset "Optimizer" begin
    err = ErrorException(
        "To use Mosek with JuMP (or MathOptInterface), you need to use the " *
        "package `MosekTools` (via `using MosekTools`). You may need to first" *
        " install it via `import Pkg; Pkg.add(\"MosekTools\")`.")
    @test_throws err Mosek.Optimizer()
end


text:
#=

ESSE wrapper

Ignacio Quintero Mächler

t(-_-t)

September 26 2017

=#

"""
    esse(states_file ::String,
         tree_file   ::String,
         envdata_file::String,
         cov_mod     ::NTuple{M,String},
         out_file    ::String,
         h           ::Int64;
         constraints ::NTuple{N,String}  = (" ",),
         mvpars      ::NTuple{O,String}  = ("lambda = beta",),
         niter       ::Int64             = 10_000,
         nthin       ::Int64             = 10,
         nburn       ::Int64             = 200,
         nchains     ::Int64             = 1,
         ntakew      ::Int64             = 100,
         winit       ::Float64             = 2.0,
         scale_y     ::NTuple{2,Bool}    = (true, false),
         algorithm   ::String            = "pruning",
         λpriors     ::Float64           = .1,
         μpriors     ::Float64           = .1,
         gpriors     ::Float64           = .1,
         lpriors     ::Float64           = .1,
         qpriors     ::Float64           = .1,
         βpriors     ::NTuple{2,Float64} = (0.0, 10.0),
         hpriors     ::Float64           = .1,
         optimal_w   ::Float64           = 0.8,
         screen_print::Int64             = 5,
         Eδt         ::Float64           = 1e-3,
         ti          ::Float64           = 0.0,
         ρ           ::Array{Float64,1}  = [1.0]) where {M,N,O}

Wrapper for running a SSE model from file.
"""
function esse(states_file ::String,
              tree_file   ::String,
              envdata_file::String,
              cov_mod     ::NTuple{M,String},
              out_file    ::String,
              h           ::Int64;
              constraints ::NTuple{N,String}  = (" ",),
              mvpars      ::NTuple{O,String}  = ("lambda = beta",),
              niter       ::Int64             = 10_000,
              nthin       ::Int64             = 10,
              nburn       ::Int64             = 200,
              nchains     ::Int64             = 1,
              ntakew      ::Int64             = 100,
              winit       ::Float64             = 2.0,
              scale_y     ::NTuple{2,Bool}    = (true, false),
              algorithm   ::String            = "pruning",
              λpriors     ::Float64           = .1,
              μpriors     ::Float64           = .1,
              gpriors     ::Float64           = .1,
              lpriors     ::Float64           = .1,
              qpriors     ::Float64           = .1,
              βpriors     ::NTuple{2,Float64} = (0.0, 10.0),
              hpriors     ::Float64           = .1,
              optimal_w   ::Float64           = 0.8,
              screen_print::Int64             = 5,
              Eδt         ::Float64           = 1e-3,
              ti          ::Float64           = 0.0,
              ρ           ::Array{Float64,1}  = [1.0]) where {M,N,O}

  # read data
  tv, ed, el, bts, x, y =
    read_data_esse(states_file, tree_file, envdata_file)

  @info "Data for $(length(tv)) species successfully read"

  # scale y
  if scale_y[1]
    # if scale each function separately or together
    if scale_y[2]
      ymin = minimum(y)
      ymax = maximum(y)
      for j in axes(y,2), i in axes(y,1)
        y[i,j] = (y[i,j] - ymin)/(ymax - ymin)
      end
    else
      ymin = minimum(y, dims = 1)
      ymax = maximum(y, dims = 1)
      for j in axes(y,2), i in axes(y,1)
        y[i,j] = (y[i,j] - ymin[j])/(ymax[j] - ymin[j])
      end
    end
  end

  # prepare data
  X, p, fp, trios, ns, ned, pupd, phid, nnps, nps, mvps, nngps, mvhfs,
  dcp, dcfp, pardic, k, h, ny, model, af!, assign_hidfacs!, abts, bts, E0 =
        prepare_data(cov_mod, tv, x, y, ed, el, ρ, h, constraints, mvpars)

  @info "Data successfully prepared"

  ## make likelihood function
  # flow algorithm
  if occursin(r"^[f|F][A-za-z]*", algorithm)

    # prepare likelihood
    Gt, Et, lbts, nets, λevent!, rootll =
      prepare_ll(p, bts, E0, k, h, ny, ns, ned, model, Eδt, ti, abts, af!)

    # make likelihood function
    llf = make_loglik(Gt, Et, X, trios, lbts, bts, ns, ned, nets,
                      λevent!, rootll)
  # prunning algorithm
  elseif occursin(r"^[p|P][A-za-z]*", algorithm)

    # prepare likelihood
    X, int, λevent!, rootll, abts1, abts2 =
      prepare_ll(X, p, E0, ns, k, h, ny, model, abts ,af!)

    # make likelihood function
    llf = make_loglik(X, abts1, abts2, trios, int,
      λevent!, rootll, k, h, ns, ned)

  else
    @error "No matching likelihood for algorithm: $algorithm"
  end

  # create prior function
  lpf = make_lpf(pupd, phid,
    λpriors, μpriors, gpriors, lpriors, qpriors, βpriors, hpriors,
    k, h, ny, model)

  # create posterior functions
  lhf = make_lhf(llf, lpf, assign_hidfacs!, dcp, dcfp)

  # number of parameters
  npars = length(pardic)

  # number of samples
  nlogs = fld(niter,nthin)

  # if parallel
  if nchains > 1
    # where to write in the Shared Array
    cits = [(1+j):(nlogs+j) for j in 0:nlogs:(nchains-1)*nlogs]

    # run slice-sampling in parallel
    R = SharedArray{Float64,2}(nlogs*nchains, npars+2)

    # run parallel loop
    @sync @distributed for ci in Base.OneTo(nchains)
      R[cits[ci],:] =
        slice_sampler(lhf, p, fp, nnps, nps, phid, mvps, nngps, mvhfs, npars,
          niter, nthin, nburn, ntakew, winit, optimal_w, screen_print)
    end

    # write output
    write_ssr(R, pardic, out_file, cits)
  else

    R = slice_sampler(lhf, p, fp, nnps, nps, phid, mvps, nngps, mvhfs, npars,
          niter, nthin, nburn, ntakew, winit, optimal_w, screen_print)

    # write output
    write_ssr(R, pardic, out_file)
  end

  return R
end

"""
    esse(tv          ::Dict{Int64,Array{Float64,1}},
         ed          ::Array{Int64,2},
         el          ::Array{Float64,1},
         x           ::Array{Float64,1},
         y           ::Array{Float64,L},
         cov_mod     ::NTuple{M,String},
         out_file    ::String,
         h           ::Int64;
         constraints ::NTuple{N,String}  = (" ",),
         mvpars      ::NTuple{O,String}  = ("lambda = beta",),
         niter       ::Int64             = 10_000,
         nthin       ::Int64             = 10,
         nburn       ::Int64             = 200,
         nchains     ::Int64             = 1,
         ntakew      ::Int64             = 100,
         winit       ::Float64             = 2.0,
         scale_y     ::NTuple{2,Bool}    = (true, false),
         algorithm   ::String            = "pruning",
         λpriors     ::Float64           = .1,
         μpriors     ::Float64           = .1,
         gpriors     ::Float64           = .1,
         lpriors     ::Float64           = .1,
         qpriors     ::Float64           = .1,
         βpriors     ::NTuple{2,Float64} = (0.0, 10.0),
         hpriors     ::Float64           = .1,
         optimal_w   ::Float64           = 0.8,
         screen_print::Int64             = 5,
         Eδt         ::Float64           = 1e-3,
         ti          ::Float64           = 0.0,
         ρ           ::Array{Float64,1}  = [1.0]) where {L,M,N,O}

Wrapper for running a SSE model from simulations.
"""
function esse(tv          ::Dict{Int64,Array{Float64,1}},
              ed          ::Array{Int64,2},
              el          ::Array{Float64,1},
              x           ::Array{Float64,1},
              y           ::Array{Float64,L},
              cov_mod     ::NTuple{M,String},
              out_file    ::String,
              h           ::Int64;
              constraints ::NTuple{N,String}  = (" ",),
              mvpars      ::NTuple{O,String}  = ("lambda = beta",),
              niter       ::Int64             = 10_000,
              nthin       ::Int64             = 10,
              nburn       ::Int64             = 200,
              nchains     ::Int64             = 1,
              ntakew      ::Int64             = 100,
              winit       ::Float64             = 2.0,
              scale_y     ::NTuple{2,Bool}    = (true, false),
              algorithm   ::String            = "pruning",
              λpriors     ::Float64           = .1,
              μpriors     ::Float64           = .1,
              gpriors     ::Float64           = .1,
              lpriors     ::Float64           = .1,
              qpriors     ::Float64           = .1,
              βpriors     ::NTuple{2,Float64} = (0.0, 10.0),
              hpriors     ::Float64           = .1,
              optimal_w   ::Float64           = 0.8,
              screen_print::Int64             = 5,
              Eδt         ::Float64           = 1e-3,
              ti          ::Float64           = 0.0,
              ρ           ::Array{Float64,1}  = [1.0]) where {L,M,N,O}

  # prepare data
  X, p, fp, trios, ns, ned, pupd, phid, nnps, nps, mvps, nngps, mvhfs,
  dcp, dcfp, pardic, k, h, ny, model, af!, assign_hidfacs!, abts, bts, E0 =
        prepare_data(cov_mod, tv, x, y, ed, el, ρ, h, constraints, mvpars)

  @info "Data successfully prepared"

  ## make likelihood function
  # flow algorithm
  if occursin(r"^[f|F][A-za-z]*", algorithm)

    # prepare likelihood
    Gt, Et, lbts, nets, λevent!, rootll =
      prepare_ll(p, bts, E0, k, h, ny, ns, ned, model, Eδt, ti, abts, af!)

    # make likelihood function
    llf = make_loglik(Gt, Et, X, trios, lbts, bts, ns, ned, nets,
                      λevent!, rootll)
  # pruning algorithm
  elseif occursin(r"^[p|P][A-za-z]*", algorithm)

    # prepare likelihood
    X, int, λevent!, rootll, abts1, abts2 =
      prepare_ll(X, p, E0, ns, k, h, ny, model, abts ,af!)

    # make likelihood function
    llf = make_loglik(X, abts1, abts2, trios, int,
      λevent!, rootll, k, h, ns, ned)

  else
    @error "No matching likelihood for algorithm: $algorithm"
  end

  # create prior function
  lpf = make_lpf(pupd, phid,
    λpriors, μpriors, gpriors, lpriors, qpriors, βpriors, hpriors,
    k, h, ny, model)

  # create posterior functions
  lhf = make_lhf(llf, lpf, assign_hidfacs!, dcp, dcfp)

  # number of parameters
  npars = length(pardic)

  # number of samples
  nlogs = fld(niter,nthin)

  # if parallel
  if nchains > 1
    # where to write in the Shared Array
    cits = [(1+j):(nlogs+j) for j in 0:nlogs:(nchains-1)*nlogs]

    # run slice-sampling in parallel
    R = SharedArray{Float64,2}(nlogs*nchains, npars+2)

    # run parallel loop
    @sync @distributed for ci in Base.OneTo(nchains)
      R[cits[ci],:] =
        slice_sampler(lhf, p, fp, nnps, nps, phid, mvps, nngps, mvhfs, npars,
          niter, nthin, nburn, ntakew, winit, optimal_w, screen_print)
    end

    # write output
    write_ssr(R, pardic, out_file, cits)
  else

    R = slice_sampler(lhf, p, fp, nnps, nps, phid, mvps, nngps, mvhfs, npars,
          niter, nthin, nburn, ntakew, winit, optimal_w, screen_print)

    # write output
    write_ssr(R, pardic, out_file)
  end

  return R
end

"""
    read_data_esse(states_file ::String,
                   tree_file   ::String,
                   envdata_file::String)

Process tree and state and environmental data file to run ESSE.
"""
function read_data_esse(states_file ::String,
                        tree_file   ::String,
                        envdata_file::String)

  # read tree in postorder and assign to objects
  tree, bts = read_tree(tree_file, order = "postorder", branching_times = true)
  ntip = tree.nnod + 1
  ed   = tree.ed
  el   = tree.el
  tlab = tree.tlab

  # assign tip labels to edge numbers
  tip_labels = Dict{String,Integer}()
  ii = 0
  for i in Base.OneTo(size(ed,1))
    if ed[i,2] <= ntip
      ii += 1
      tip_labels[tlab[ii]] = ed[i,2]
    end
  end

  # read states text file
  data = readdlm(states_file)

  if size(data,1) != ntip
    data = readdlm(states_file, '\t', '\r')
  end

  if size(data,1) != ntip
    data = readdlm(states_file, '\t', '\n')
  end

  if size(data,1) != ntip
    error("Data file cannot be made of the right dimensions.\n Make sure the data file has the same number of rows as tips in the tree")
  end

  data_tlab    = convert(Array{String,1}, data[:,1])
  data_states  = convert(Array{Float64,2},  data[:,2:end])

  # create dictionary
  tip_states = Dict(tip_labels[val] => data_states[i,:]
                   for (i,val) = enumerate(data_tlab))

  # process environmental data file
  envdata = readdlm(envdata_file)

  x = envdata[:,1]
  y = envdata[:,2:end]

  return tip_states, ed, el, bts, x, y
end

"""
  write_ssr(R       ::Array{Float64,2},
            pardic  ::Dict{String,Int64},
            out_file::String)

Write the samples from an MC sampler data frame
given a Dictionary of parameters.
"""
function write_ssr(R       ::Array{Float64,2},
                   pardic  ::Dict{String,Int64},
                   out_file::String)

  # column names
  col_nam = ["Iteration", "Posterior"]

  for (k,v) in sort!(collect(pardic), by = x -> x[2])
    push!(col_nam, k)
  end

  R = vcat(reshape(col_nam, 1, lastindex(col_nam)), R)

  writedlm(out_file*".log", R)
end

"""
  write_ssr(R       ::SharedArray{Float64,2},
            pardic  ::Dict{String,Int64},
            out_file::String,
            cits    ::Array{UnitRange{Int64},1})

Write the samples from multiple chains of MC sampler data frame
given a Dictionary of parameters.
"""
function write_ssr(R       ::SharedArray{Float64,2},
                   pardic  ::Dict{String,Int64},
                   out_file::String,
                   cits    ::Array{UnitRange{Int64},1})

  # column names
  col_nam = ["Iteration", "Posterior"]

  for (k,v) in sort!(collect(pardic), by = x -> x[2])
    push!(col_nam, k)
  end

  for ci in Base.OneTo(length(cits))
    ri = vcat(reshape(col_nam, 1, lastindex(col_nam)), R[cits[ci],:])
    writedlm(out_file*"_chain_$ci.log", ri)
  end

end

max_stars_repo_name: hmorlon/JPANDA

max_issues_repo_name: hmorlon/JPANDA

max_forks_repo_name: hmorlon/JPANDA


text:
struct TripleF64
   value::Tuple{Float64, Float64, Float64}
end

struct TripleF32
   value::Tuple{Float32, Float32, Float32}
end

TripleF64(x::Float64) = TripleF64(x, 0.0, 0.0)
TripleF64(x::Float64, y::Float64) = TripleF64(add_2(x,y)..., 0.0)
TripleF64(x::Float32) = TripleF64(Float64(x), 0.0, 0.0)
TripleF64(x::Float32, y::Float32) = TripleF64(add_2(Float64(x),Float64(y))..., 0.0)
TripleF64(x::Float64, y::Float32) = TripleF64(add_2(x,Float64(y))..., 0.0)
TripleF64(x::Float32, y::Float64) = TripleF64(add_2(Float64(x),y)..., 0.0)

TripleF32(x::Float32) = TripleF32(x, 0.0f0, 0.0f0)
TripleF32(x::Float32, y::Float32) = TripleF64(add_2(x,y)..., 0.0f0)

TripleF64(x::Triple32) = TripleF64(add_3(Float64(x.value[1]),(Float64(x.value[2]),(Float64(x.value[3]))...,)


text:
module ShunnHamQuadrature

using LinearAlgebra
using StaticArrays

include("Quadrature/Triangle_ref.jl")
include("Quadrature/Triangle.jl")

include("Quadrature/Tetrahedron_ref.jl")
include("Quadrature/Tetrahedron.jl")

include("Quadrature/Pentatope_ref.jl")
include("Quadrature/Pentatope.jl")

include("Quadrature/Hexateron.jl")

export(QScheme)

"""
    QScheme
A quadrature scheme (points and weights). Schemes are created by calling
`shunnham*D` gives the points in bariycentric coordinates. With 'get_pts_wts'
they can be converted in cartisian coordinates for a given simplex.
"""
struct QScheme{N,T}
    weights::Vector{T}
    points::Vector{SVector{N,T}}
end

@inbounds function calc_vol(vertices::SMatrix{D,N,T}) where {D,N,T}
    @assert N == D + 1
    X = SMatrix{D,D,T}(vertices[i, j + 1] - vertices[i, 1]
                       for i in 1:D, j in 1:D)
    vol = det(X)
    return vol
end

export get_pts_wts

function get_pts_wts(scheme::QScheme{N,T},  vertices::SMatrix{D,N,U}) where {N,T,D,U}
    @assert N > 0
    @assert N >= D + 1

    ws = scheme.weights
    ps = scheme.points
    @assert length(ws) == length(ps)

    xs  = Vector{SVector{D,T}}()
    for i in 1:length(ws)

        p = ps[i]

        push!(xs,vertices * p)

    end

    vol = calc_vol(vertices) / factorial(N - 1)

    return xs, ws*vol
end

function get_pts_wts(scheme::QScheme{N,T}, vertices::SVector{N,SVector{D,U}}) where {N,T,D,U}
    return get_pts_wts( scheme,SMatrix{D,N,U}(vertices[n][d] for d in 1:D, n in 1:N))
end

function get_pts_wts( scheme::QScheme{N}, vertices::AbstractVector) where {N}
    @assert length(vertices) == N
    @assert N > 0
    D = length(vertices[1])
    @assert N >= D + 1
    vertices′ = SVector{N}(map(SVector{D}, vertices))
    return get_pts_wts(scheme, vertices′)
end

export shunnham5D

export shunnham4D
export shunnham4D_ref
export shunnham3D
export shunnham3D_ref
export shunnham2D
export shunnham2D_ref

"""
    shunnhamXD(n)
Returns the n-th quadrature rule on the X-dimensional simplex. Returns a Qudarature Scheme with
the quadrature points in barycentric coordiantes and  normalized quadrature weights.
"""

function shunnham5D(n)
    @assert 1 <= n <= length(hexateronAn)
    p = hexateronAn[n]
    w = hexateronWn[n]

    return QScheme(w, p)
end

function shunnham4D_ref(n)
    @assert 1 <= n <= length(pentatopeA)
    p = pentatopeA[n]
    w = pentatopeW[n]

    return QScheme(w, p)
end

function shunnham4D(n)
    @assert 1 <= n <= length(pentatopeAn)
    p = pentatopeAn[n]
    w = pentatopeWn[n]

    return QScheme(w, p)
end

function shunnham3D_ref(n)
    @assert 1 <= n <= length(tetrahedronA)
    p = tetrahedronA[n]
    w = tetrahedronW[n]

    return QScheme(w, p)
end

function shunnham3D(n)
    @assert 1 <= n <= length(tetrahedronAn)
    p = tetrahedronAn[n]
    w = tetrahedronWn[n]

    return QScheme(w, p)
end

function shunnham2D_ref(n)
    @assert 1 <= n <= length(triangleA)
    p = triangleA[n]
    w = triangleW[n]

    return QScheme(w, p)
end

function shunnham2D(n)
    @assert 1 <= n <= length(triangleAn)
    p = triangleAn[n]
    w = triangleWn[n]

    return QScheme(w, p)
end

end


text:
using JuMP
using Gurobi
using Plasmo
using PlasmoAlgorithms

##Place MP and SP into ModelGraph
mp = Model(solver = GurobiSolver())
sp = Model(solver = GurobiSolver())

@variable(mp, x[1:2], category = :Bin)
@constraint(mp, x[1]+x[2]>=1)
@objective(mp, Min, 2x[1]+3x[2])

@variable(sp, y[1:2], category = :Bin)
@variable(sp, x[1:2], category = :Bin)
@constraint(sp, x[1]+x[2]+y[1]>=2)
@constraint(sp, y[1]+y[2]<=2)
@objective(sp, Min, y[1]+y[2])

## Plasmo Graph
g = ModelGraph()
setsolver(g, GurobiSolver())
n1 = add_node(g)
setmodel(n1,mp)
n2 = add_node(g)
setmodel(n2,sp)

##Set n2 as a child node of n1
edge = Plasmo.add_edge(g,n1,n2)

## Linking constraints between MP and SP
@linkconstraint(g,[i in 1:2], n1[:x][i] == n2[:x][i])

r = bendersolve(g)


text:
module TreeverseAlgorithm

using Requires

include("treeverse.jl")

function __init__()
    @require Viznet = "52a3aca4-6234-47fd-b74a-806bdf78ede9" begin
        include("visualize.jl")
    end
end

end


text:
using BinaryBuilder

# Collection of sources required to build Nettle
name = "ntl"
version = v"10.5.0"
sources = [
    "https://www.shoup.net/ntl/ntl-10.5.0.tar.gz" =>
    "b90b36c9dd8954c9bc54410b1d57c00be956ae1db5a062945822bbd7a86ab4d2",
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/ntl-*/src
./configure "PREFIX=${prefix}" "GMP_PREFIX=${prefix}" NATIVE=off SHARED=on TUNE=x86 CXX="ccache ${CXX}"
make -j${nproc}
make install
"""

# Bootstrapping problem; only do natively-runnable platforms
platforms = [
    Linux(:x86_64),
    Linux(:i686),
    Linux(:x86_64; libc=:musl),
]

# The products that we will ensure are always built
products(prefix) = [
    LibraryProduct(prefix, "libntl", :libntl),
]

# Dependencies that must be installed before this package can be built
dependencies = [
    "https://github.com/JuliaMath/GMPBuilder/releases/download/v6.1.2-2/build_GMP.v6.1.2.jl",
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies)

max_stars_repo_name: miguelraz/Yggdrasil

max_issues_repo_name: miguelraz/Yggdrasil

max_forks_repo_name: miguelraz/Yggdrasil


text:
using RDatasets, StatsPlots; pyplot()

iris = dataset("datasets", "iris")
@df iris andrewsplot(:Species, cols(1:4),
	line=(fill=[:blue :red :green]), legend=:topleft)

max_stars_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook

max_issues_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook

max_forks_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook


text:
function loadbutton(ui)
    s = filepicker(placeholder="Load file", accept=".csv")
    on(t -> set_ui!(ui, t), observe(s)) # print to console on every update
    s
end

function loadfrommemory(ui)
    tables = readdir(tablefolder)
    d = dropdown(tables, placeholder="Previously saved", value = nothing)
    on(x -> x!="" && set_ui!(ui, JuliaDB.load(joinpath(tablefolder, x))), observe(d))
    d
end