Clij CL_out of ressources

Hi,
When I run my plugin using clij2 I got an error message on a Windows workstation about clclear that I didn’t have on my linux workstation. The windows workstation configuration :

Available CL backends:

  • net.haesleinhuepf.clij.clearcl.backend.jocl.ClearCLBackendJOCL@784e70da
    Functional backend:null
    Best backend:net.haesleinhuepf.clij.clearcl.backend.jocl.ClearCLBackendJOCL@3d64b1fe
    Used CL backend: net.haesleinhuepf.clij.clearcl.backend.jocl.ClearCLBackendJOCL@5e053f0e
    ClearCL: ClearCLBase [mClearCLBackendInterface=net.haesleinhuepf.clij.clearcl.backend.jocl.ClearCLBackendJOCL@5e053f0e, mPeerPointer=null]
    Number of platforms:1
    [0] NVIDIA CUDA
    Number of devices: 1
    Available devices:
    [0] GeForce GTX TITAN X
    NumberOfComputeUnits: 24
    Clock frequency: 1076
    Version: 1.2
    Extensions: cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_d3d10_sharing cl_khr_d3d10_sharing cl_nv_d3d11_sharing cl_nv_copy_opts cl_khr_gl_event cl_nv_create_buffer cl_khr_int64_base_atomics cl_khr_int64_extended_atomics cl_khr_device_uuid
    GlobalMemorySizeInBytes: 12884901888
    LocalMemorySizeInBytes: 49152
    MaxMemoryAllocationSizeInBytes: 3221225472
    MaxWorkGroupSize: 1024

Exception: net.haesleinhuepf.clij.clearcl.exceptions.ClearCLTooManyContextsException: Too many contexts have been created and not released

The error message :

(Fiji Is Just) ImageJ 2.1.0/1.53h; Java 1.8.0_281 [64-bit]; Windows 10 10.0; 874MB of 50000MB (1%)

net.haesleinhuepf.clij.clearcl.exceptions.OpenCLException: OpenCL error: -5 → CL_OUT_OF_RESOURCES
at net.haesleinhuepf.clij.clearcl.backend.BackendUtils.checkOpenCLError(BackendUtils.java:346)
at net.haesleinhuepf.clij.clearcl.backend.jocl.ClearCLBackendJOCL.lambda$enqueueWriteToBuffer$25(ClearCLBackendJOCL.java:813)
at net.haesleinhuepf.clij.clearcl.backend.BackendUtils.checkExceptions(BackendUtils.java:171)
at net.haesleinhuepf.clij.clearcl.backend.jocl.ClearCLBackendJOCL.enqueueWriteToBuffer(ClearCLBackendJOCL.java:811)
at net.haesleinhuepf.clij.clearcl.ClearCLBuffer.readFrom(ClearCLBuffer.java:511)
at net.haesleinhuepf.clij.clearcl.ClearCLBuffer.readFrom(ClearCLBuffer.java:477)
at net.haesleinhuepf.clij.converters.implementations.ImagePlusToClearCLBufferConverter.convert(ImagePlusToClearCLBufferConverter.java:142)
at net.haesleinhuepf.clij.converters.implementations.ImagePlusToClearCLBufferConverter.convert(ImagePlusToClearCLBufferConverter.java:24)
at net.haesleinhuepf.clij.CLIJ.convert(CLIJ.java:475)
at net.haesleinhuepf.clij2.CLIJ2.push(CLIJ2.java:96)
at Liver_fibrosis_CD_tools.Liver_fibrosis_tools.clij_analyse(Liver_fibrosis_tools.java:197)
at Liver_fibrosis_CD.Liver_fibrosis.run(Liver_fibrosis.java:186)
at ij.IJ.runUserPlugIn(IJ.java:241)
at ij.IJ.runPlugIn(IJ.java:204)
at ij.Executer.runCommand(Executer.java:151)
at ij.Executer.run(Executer.java:69)
at java.lang.Thread.run(Unknown Source)

Thanks
Philippe

Hi @pmailly

this error message:

… suggests that you re-initialized CLIJ withouth closing it properly. Can you confirm that your are using CLIJ approximately like this:

CLIJ2 clij2 = CLIJ2.getInstance();

// do something with CLIJ...

If you want to share a piece of code, that might be helpful as well.

Let me know if this helps!

Cheers,
Robert

Hi Robert,
Yes I use I think the correct CLIJ instance , here some code. However, why the code is running correctly on one machine and not on another ???

ackage Liver_fibrosis_CD_tools;




import static Liver_fibrosis_CD.Liver_fibrosis.saveMask;
import static Liver_fibrosis_CD.Liver_fibrosis.tileSize;
import fiji.util.gui.GenericDialogPlus;
import ij.IJ;
import ij.ImagePlus;
import ij.io.FileSaver;
import ij.measure.Measurements;
import ij.measure.ResultsTable;
import ij.plugin.Duplicator;
import ij.plugin.filter.Analyzer;
import static ij.plugin.filter.Analyzer.setOption;
import ij.plugin.filter.ParticleAnalyzer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import net.haesleinhuepf.clij.clearcl.ClearCLBuffer;
import net.haesleinhuepf.clij2.CLIJ2;

        
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose dots_Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author phm
 */
public class Liver_fibrosis_tools {    
    
    
    
    public static CLIJ2 clij2 = CLIJ2.getInstance();
    
    
    
     /**
     * check  installed modules
     * @return 
     */
    public static boolean checkInstalledModules() {
        // check install
        ClassLoader loader = IJ.getClassLoader();
        try {
            loader.loadClass("net.haesleinhuepf.clij2.CLIJ2");
        } catch (ClassNotFoundException e) {
            IJ.log("CLIJ not installed, please install from update site");
            return false;
        }
        try {
            loader.loadClass("mcib3d.geom");
        } catch (ClassNotFoundException e) {
            IJ.log("3D ImageJ Suite not installed, please install from update site");
            return false;
        }
        return true;
    }

    /**
     * Find serie from roi filename
     * @param files
     * @param fileName
     * @return 
     */
    public static String find_roi(String[] files, String fileName) {
        String series = "";
        for (String f : files) {
            if (f.matches(fileName+"#.*.zip"))
                series = f.split("#")[1];
	}
	return (series);
    }
    
    /**
     * Dialog 

     */
    public static String dialog() {
        String dir = "";
        GenericDialogPlus gd = new GenericDialogPlus("Parameters");
        gd.addDirectoryField("Choose Directory Containing Image Files : ", "");
        gd.addNumericField("Tile size : ", tileSize, 0);
        gd.addCheckbox(" Save mask", saveMask);
        gd.showDialog();
        dir = gd.getNextString();
        tileSize = gd.getNextNumber();
        saveMask = gd.getNextBoolean();
        return(dir);
    }
    
     /**  
     * median 2D box filter
     * Using CLIJ2
     * @param imgCL
     * @param sizeX
     * @param sizeY
     * @param sizeZ
     * @return imgOut
     */ 
    public static ClearCLBuffer medianFilter(ClearCLBuffer imgCL, double sizeX, double sizeY) {
        ClearCLBuffer imgOut = clij2.create(imgCL);
        clij2.median2DBox(imgCL, imgOut, sizeX, sizeY);
        return(imgOut);
    }
    
    
     /**  
     * exponentiel box filter
     * Using CLIJ2
     * @param imgCL
     * @param sizeX
     * @param sizeY
     * @return imgOut
     */ 
    public static ClearCLBuffer expoFilter(ClearCLBuffer imgCL, double sizeX, double sizeY) {
        ClearCLBuffer imgIn = clij2.push(imgCL);
        ClearCLBuffer imgOut = clij2.create(imgIn);
        clij2.exponential(imgIn, imgOut);
        return(imgOut);
    }
    
     /**  
     * minimum box filter
     * Using CLIJ2
     * @param imgCL
     * @param sizeX
     * @param sizeY
     * @return imgOut
     */ 
    public static ClearCLBuffer minFilter(ClearCLBuffer imgCL, double sizeX, double sizeY) {
        ClearCLBuffer imgIn = clij2.push(imgCL);
        ClearCLBuffer imgOut = clij2.create(imgIn);
        clij2.minimum2DBox(imgIn, imgOut, sizeX, sizeY);
        return(imgOut);
    }
    
    
  /**
   * Open
   * USING CLIJ2
   * @param imgCL
   * @return imgCLOut
   */
    private static ClearCLBuffer open(ClearCLBuffer imgCL, int iter) {
        ClearCLBuffer imgCLOut = clij2.create(imgCL);
        clij2.openingBox(imgCL, imgCLOut, iter);
        return(imgCLOut);
    }
    
    /**
     * Threshold 
     * USING CLIJ2
     * @param imgCL
     * @param thMed
     */
    public static ClearCLBuffer threshold(ClearCLBuffer imgCL, String thMed) {
        ClearCLBuffer imgCLBin = clij2.create(imgCL);
        clij2.automaticThreshold(imgCL, imgCLBin, thMed);
        return(imgCLBin);
    }
    
  
    
    public static double clij_analyse(ImagePlus img, String path ) {
        double fibArea = 0;
        ResultsTable rt = new ResultsTable();
        ClearCLBuffer imgCL = clij2.push(img);
        ClearCLBuffer imgCLmed = medianFilter(imgCL, 4, 4);
        clij2.release(imgCL);
        ClearCLBuffer imgCLexp = clij2.create(imgCLmed);
        clij2.exponential(imgCLmed, imgCLexp);
        clij2.release(imgCLmed);
        ClearCLBuffer imgCLbin = threshold(imgCLexp, "Triangle");
        clij2.release(imgCLexp);
        ClearCLBuffer imgCLopen = open(imgCLbin, 4);
        clij2.release(imgCLbin);
        ImagePlus imgBin = clij2.pull(imgCLopen);
        clij2.release(imgCLopen);
        imgBin.setCalibration(img.getCalibration());
        IJ.setAutoThreshold(imgBin, "Triangle dark");
//        setOption("BlackBackground", false);
//        IJ.run(imgBin, "Convert to Mask", "");
        ParticleAnalyzer ana = new ParticleAnalyzer(ParticleAnalyzer.CLEAR_WORKSHEET+ParticleAnalyzer.SHOW_ROI_MASKS,
                Measurements.AREA+Measurements.LIMIT, rt, 3, Double.MAX_VALUE);
        ana.setHideOutputImage(true);
        ana.analyze(imgBin);
        for (int i = 0; i < rt.getCounter(); i++) {
            fibArea += rt.getValue("Area", i);
        }
        System.out.println("Fibrosis ="+fibArea);
        // Save masks
        if (saveMask) {
            ImagePlus imgMask = ana.getOutputImage();
            IJ.run(imgMask, "Enhance Contrast...", "saturated=0.3 normalize");
            FileSaver imgSave = new FileSaver(imgMask);
            imgSave.saveAsTiff(path);
            imgMask.close();
        }
        imgBin.close();
        return(fibArea);
    }    
Thanks
Philippe
1 Like

Ok, the code looks good, indeed.

There are multiple reasons: Operating System, GPU Hardware, driver versions, other software running in the background.

As a first guess: Can you compare installed Nvidia driver versions?

The “good” machine is an Linux Ubuntu 20.10 with NVIDIA Quadro RTX 300 ((460.27 driver version) and the “bad” a Windows 10 with NVIDIA Titan X (461.09 driver version) the GPU are not the same it’s difficult to compare. I don’t thing it’s the code because the error come also with the macro CLI Info.

Philippe

Can you re-install the driver on the machine where it’s not working? Furthermore, do simple ImageJ macro using clij work?

If I open an image (600x600) and do a median filter 2x2 :
(Fiji Is Just) ImageJ 2.1.0/1.53h; Java 1.8.0_281 [64-bit]; Windows 10 10.0; 558MB of 50000MB (1%)

java.lang.NullPointerException
at net.haesleinhuepf.clij2.CLIJ2.execute(CLIJ2.java:424)
at net.haesleinhuepf.clij2.plugins.Median2DBox.median2DBox(Median2DBox.java:69)
at net.haesleinhuepf.clij2.CLIJ2Ops.median2DBox(CLIJ2Ops.java:4439)
at net.haesleinhuepf.clij2.plugins.Median2DBox.executeCL(Median2DBox.java:51)
at net.haesleinhuepf.clij.macro.AbstractCLIJPlugin.run(AbstractCLIJPlugin.java:478)
at ij.plugin.filter.PlugInFilterRunner.processOneImage(PlugInFilterRunner.java:265)
at ij.plugin.filter.PlugInFilterRunner.(PlugInFilterRunner.java:114)
at ij.IJ.runUserPlugIn(IJ.java:243)
at ij.IJ.runPlugIn(IJ.java:204)
at ij.Executer.runCommand(Executer.java:151)
at ij.Executer.run(Executer.java:69)
at java.lang.Thread.run(Unknown Source)
*

:7:26: warning: unknown OpenCL extension ‘cl_amd_printf’ - ignoring
#pragma OPENCL EXTENSION cl_amd_printf : enable
^
:590:71: error: passing ‘int attribute((ext_vector_type(2)))’ to parameter of incompatible type ‘int4’
array[count] = (IMAGE_dst_PIXEL_TYPE)READ_src_IMAGE(src,sampler,coord+((int2){x,y})).x;
^~~~~~~~~~~~~~~~~~~
:519:110: note: expanded from macro ‘READ_src_IMAGE’
#define READ_src_IMAGE(a,b,c) read_buffer3duc(GET_IMAGE_WIDTH(a),GET_IMAGE_HEIGHT(a),GET_IMAGE_DEPTH(a),a,b,c)
^
:197:157: note: passing argument to parameter ‘position’ here
inline uchar2 read_buffer3duc(long read_buffer_width, long read_buffer_height, long read_buffer_depth, __global uchar * buffer_var, sampler_t sampler, int4 position )
^
:598:24: error: passing ‘const attribute((address_space(16776963))) int2’ to parameter of incompatible type ‘int4’
WRITE_dst_IMAGE(dst, coord, res);
^~~~~
:515:110: note: expanded from macro ‘WRITE_dst_IMAGE’
#define WRITE_dst_IMAGE(a,b,c) write_buffer3duc(GET_IMAGE_WIDTH(a),GET_IMAGE_HEIGHT(a),GET_IMAGE_DEPTH(a),a,b,c)
^
:278:140: note: passing argument to parameter ‘pos’ here
inline void write_buffer3duc(long write_buffer_width, long write_buffer_height, long write_buffer_depth, __global uchar * buffer_var, int4 pos, uchar value )
^
Error when trying to create kernel median_box_2d
net.haesleinhuepf.clij.clearcl.exceptions.OpenCLException: OpenCL error: -45 → CL_INVALID_PROGRAM_EXECUTABLE
at net.haesleinhuepf.clij.clearcl.backend.BackendUtils.checkOpenCLErrorCode(BackendUtils.java:352)
at net.haesleinhuepf.clij.clearcl.backend.jocl.ClearCLBackendJOCL.lambda$getKernelPeerPointer$19(ClearCLBackendJOCL.java:601)
at net.haesleinhuepf.clij.clearcl.backend.BackendUtils.checkExceptions(BackendUtils.java:156)
at net.haesleinhuepf.clij.clearcl.backend.jocl.ClearCLBackendJOCL.getKernelPeerPointer(ClearCLBackendJOCL.java:593)
at net.haesleinhuepf.clij.clearcl.ClearCLCompiledProgram.createKernel(ClearCLCompiledProgram.java:137)
at net.haesleinhuepf.clij.clearcl.ClearCLProgram.createKernel(ClearCLProgram.java:685)
at net.haesleinhuepf.clij.clearcl.util.CLKernelExecutor.getKernel(CLKernelExecutor.java:353)
at net.haesleinhuepf.clij.clearcl.util.CLKernelExecutor.enqueue(CLKernelExecutor.java:229)
at net.haesleinhuepf.clij2.CLIJ2.lambda$executeSubsequently$0(CLIJ2.java:466)
at net.haesleinhuepf.clij.clearcl.util.ElapsedTime.measure(ElapsedTime.java:97)
at net.haesleinhuepf.clij.clearcl.util.ElapsedTime.measure(ElapsedTime.java:28)
at net.haesleinhuepf.clij2.CLIJ2.executeSubsequently(CLIJ2.java:456)
at net.haesleinhuepf.clij2.CLIJ2.executeSubsequently(CLIJ2.java:443)
at net.haesleinhuepf.clij2.CLIJ2.executeSubsequently(CLIJ2.java:438)
at net.haesleinhuepf.clij2.CLIJ2.execute(CLIJ2.java:423)
at net.haesleinhuepf.clij2.plugins.Median2DBox.median2DBox(Median2DBox.java:69)
at net.haesleinhuepf.clij2.CLIJ2Ops.median2DBox(CLIJ2Ops.java:4439)
at net.haesleinhuepf.clij2.plugins.Median2DBox.executeCL(Median2DBox.java:51)
at net.haesleinhuepf.clij.macro.AbstractCLIJPlugin.run(AbstractCLIJPlugin.java:478)
at ij.plugin.filter.PlugInFilterRunner.processOneImage(PlugInFilterRunner.java:265)
at ij.plugin.filter.PlugInFilterRunner.(PlugInFilterRunner.java:114)
at ij.IJ.runUserPlugIn(IJ.java:243)
at ij.IJ.runPlugIn(IJ.java:204)
at ij.Executer.runCommand(Executer.java:151)
at ij.Executer.run(Executer.java:69)
at java.lang.Thread.run(Unknown Source)

This part of the error message suggests you are passing a 3D image to a 2D median filter:

Can you please execute this script and let me know if it works?

newImage("Untitled", "8-bit noise", 600, 600, 1);
run("CLIJ2 Macro Extensions", "cl_device=");

// median
image1 = "Untitled";
Ext.CLIJ2_push(image1);
image2 = "median1911937808";
radius_x = 2.0;
radius_y = 2.0;
Ext.CLIJ2_median2DBox(image1, image2, radius_x, radius_y);
Ext.CLIJ2_pull(image2);
1 Like

Ok, I run the plugin on another Windows machine with no problem. So I think that the problem come from some hardware configuration.
Many thanks for helping
Philippe

1 Like