Commit a149cadd authored by Andrey Filippov's avatar Andrey Filippov
Browse files

converting to GPU

parent ac185fe7
Loading
Loading
Loading
Loading
+82 −15
Original line number Diff line number Diff line
@@ -63,7 +63,10 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import com.elphel.imagej.tileprocessor.DttRad2;
@@ -739,6 +742,13 @@ public class GPUTileProcessor {
                    Sizeof.FLOAT);                         // int ElementSizeBytes)
            texture_stride_rgba = (int)(device_stride[0] / Sizeof.FLOAT);
    	}
    	public int getTilesX() {
            return img_width / DTT_SIZE;
    	}

    	public int getTilesY() {
            return img_height / DTT_SIZE;
    	}
    	
    	public void resetGeometryCorrection() {
    		geometry_correction_set = false;
@@ -760,7 +770,6 @@ public class GPUTileProcessor {
    	}

    	public void setGeometryCorrectionVector() { // will reset geometry_correction_vector_set when running GPU kernel
//    		if (geometry_correction_vector_set) return;
    		setExtrinsicsVector(
    				quadCLT.getGeometryCorrection().getCorrVector());
    	}
@@ -1211,6 +1220,63 @@ public class GPUTileProcessor {
        	return tp_tasks;
        }
       
    	public GPUTileProcessor.TpTask[]  setTpTask(
//    			final GPUTileProcessor.GpuQuad gpuQuad,
    			final double [][]	           disparity_array,  // [tilesY][tilesX] - individual per-tile expected disparity
    			final double                   disparity_corr,
    			final boolean []               need_corrs,       // should be initialized to boolean[1] or null
    			final int [][]                 tile_op,          // [tilesY][tilesX] - what to do - 0 - nothing for this tile
    			final int                      corr_mask,        // <0 - use corr mask from the tile tile_op, >=0 - overwrite all with non-zero corr_mask_tp
    			final int                      threadsMax)       // maximal number of threads to launch
    	{
    		final int tilesX = getTilesX();
    		final int tilesY = getTilesY();
    		final AtomicInteger ai =            new AtomicInteger(0);
    		final AtomicBoolean acorrs =        new AtomicBoolean(false);
    		final List<GPUTileProcessor.TpTask> task_list = new CopyOnWriteArrayList<GPUTileProcessor.TpTask>();
    		final Thread[] threads = ImageDtt.newThreadArray(threadsMax);
    		for (int ithread = 0; ithread < threads.length; ithread++) {
    			threads[ithread] = new Thread() {
    				@Override
    				public void run() {
    					for (int nTile = ai.getAndIncrement(); nTile < tilesX * tilesY; nTile = ai.getAndIncrement()) {
    						int tileY = nTile /tilesX;
    						int tileX = nTile % tilesX;
    						//						tIndex = tileY * tilesX + tileX;
    						if (tile_op[tileY][tileX] == 0) continue; // nothing to do for this tile
    				        // which images to use
    						int img_mask =  ImageDtt.getImgMask(tile_op[tileY][tileX]);
    					    // which pairs to combine in the combo:  1 - top, 2 bottom, 4 - left, 8 - right
    						int corr_mask_tp = ImageDtt.getPairMask(tile_op[tileY][tileX]); // limited to 4 bits only!
    						if (corr_mask_tp != 0) {
    							if (corr_mask >=0) {
    								corr_mask_tp = corr_mask; 	
    							}
    							if (corr_mask_tp != 0) {
    								acorrs.set(true);
    							}
    						}
    						task_list.add(new GPUTileProcessor.TpTask(
    								tileX,
    								tileY,
    								(float) (disparity_array[tileY][tileX] + disparity_corr),
    								((img_mask  & 0x0f) << 0) |
    								((corr_mask_tp & 0x3f) << 4)
    								)); // task == 1 for now
    						// mask out pairs that use missing channels
    					}

    				}
    			};
    		}
    		ImageDtt.startAndJoin(threads);
    		if (need_corrs != null) {
    			need_corrs[0] = acorrs.get();
    		}
    		return task_list.toArray(new GPUTileProcessor.TpTask[task_list.size()]);		
    	}

        
        
        /**
         * Prepare contents pointers for calculation of the correlation pairs
@@ -1653,6 +1719,21 @@ public class GPUTileProcessor {
        	cuCtxSynchronize();
        }

        public int [] getCorrIndices() {
        	float [] fnum_corrs = new float[1];
        	cuMemcpyDtoH(Pointer.to(fnum_corrs), gpu_num_corr_tiles,  1 * Sizeof.FLOAT);
        	int num_corrs =      Float.floatToIntBits(fnum_corrs[0]);
        	float [] fcorr_indices = new float [num_corrs];
        	cuMemcpyDtoH(Pointer.to(fcorr_indices), gpu_corr_indices,  num_corrs * Sizeof.FLOAT);
        	int [] corr_indices = new int [num_corrs];
        	for (int i = 0; i < num_corrs; i++) {
        		corr_indices[i] = Float.floatToIntBits(fcorr_indices[i]);
        	}
        	num_corr_tiles = num_corrs;
        	return corr_indices;

        }

        public float [][] getCorr2D(int corr_rad){
        	int corr_size = (2 * corr_rad + 1) * (2 * corr_rad + 1);
        	float [] cpu_corrs = new float [ num_corr_tiles * corr_size];
@@ -1677,20 +1758,6 @@ public class GPUTileProcessor {
        	return corrs;
        }

        public int [] getCorrIndices() {
        	float [] fnum_corrs = new float[1];
        	cuMemcpyDtoH(Pointer.to(fnum_corrs), gpu_num_corr_tiles,  1 * Sizeof.FLOAT);
        	int num_corrs =      Float.floatToIntBits(fnum_corrs[0]);
        	float [] fcorr_indices = new float [num_corrs];
        	cuMemcpyDtoH(Pointer.to(fcorr_indices), gpu_corr_indices,  num_corrs * Sizeof.FLOAT);
        	int [] corr_indices = new int [num_corrs];
        	for (int i = 0; i < num_corrs; i++) {
        		corr_indices[i] = Float.floatToIntBits(fcorr_indices[i]);
        	}
        	num_corr_tiles = num_corrs;
        	return corr_indices;

        }

//	        
/**
+44 −10
Original line number Diff line number Diff line
@@ -60,6 +60,9 @@ public class CLTPass3d{
		public  boolean []      border_tiles =         null; // these are border tiles, zero out alpha
		public  boolean []      selected =             null; // which tiles are selected for this layer
		public  double [][][][] texture_tiles;
		public  float  [][]     texture_img =          null; // [3][] (RGB) or [4][] RGBA
		public  Rectangle       texture_woi =          null; // null or generated texture location/size
				
		public  double [][]      max_tried_disparity =  null; //[ty][tx] used for combined passes, shows maximal disparity for this tile, regardless of results
		public  boolean         is_combo =             false;
		public  boolean         is_measured =          false;
@@ -96,13 +99,27 @@ public class CLTPass3d{
		{
			return 	texture_tiles;
		}

		public float [][] getTextureImages()
		{
			return 	texture_img;
		}

		public Rectangle getTextureWoi()
		{
			return 	texture_woi;
		}
		
		public double [][] getMaxTriedDisparity()
		{
			return max_tried_disparity;
		}
		public double [][] getTileRBGA(
				int num_layers)
				int num_layers) // 4 or 12
		{
			if (texture_img != null) {
				System.out.println("FIXME: implement replacement for the GPU-generated textures (using macro mode?)");
			}
			if (texture_tiles == null) return null;
			int tilesY = texture_tiles.length;
			int tilesX = 0;
@@ -163,6 +180,22 @@ public class CLTPass3d{
			int tilesY = tileProcessor.getTilesY();
			selected = new boolean[tilesY*tilesX];
			int minX = tilesX, minY = tilesY, maxX = -1, maxY = -1;
			if (texture_img != null) { // using GPU output
//tileProcessor.getTileSize()
				if (texture_woi != null) {
					int tile_size = tileProcessor.getTileSize();
					texture_bounds = new Rectangle(
							texture_woi.x/tile_size, texture_woi.y/tile_size, texture_woi.width/tile_size, texture_woi.height/tile_size);
					// setting full rectangle as selected, not just textures? Use some other method?
					for (int ty = texture_bounds.y; ty < (texture_bounds.y + texture_bounds.height); ty++) {
						for (int tx = texture_bounds.x; tx < (texture_bounds.x + texture_bounds.width); tx++) {
							selected[ty*tilesX+tx] = true;
						}
					}
					return;
				}
			}
			if (texture_tiles != null) {
				for (int ty = 0; ty < tilesY; ty++) for (int tx = 0; tx < tilesX; tx++){
					if (texture_tiles[ty][tx] != null) {
						selected[ty * tilesX + tx] = true;
@@ -174,6 +207,7 @@ public class CLTPass3d{
						selected[ty * tilesX + tx] = false; // may be omitted
					}
				}
			}
			if (maxX < 0) {
				texture_bounds = null;
			} else {
+11428 −0

File added.

File size exceeds preview limit.

+8 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import java.util.Properties;
import com.elphel.imagej.common.GenericJTabbedDialog;

public class ImageDttParameters {
	public boolean gpu_mode_debug =         true;
	public boolean corr_mode_debug =        true;
	public boolean mix_corr_poly =          true;
	public double  min_poly_strength =      0.2; /// 0.1
@@ -191,6 +192,9 @@ public class ImageDttParameters {


	public void dialogQuestions(GenericJTabbedDialog gd) {
		
		    gd.addCheckbox    ("Debug CPU->GPU matching",                                         this.gpu_mode_debug,
				"output clt_corr_partial");
			gd.addCheckbox    ("Enable ImageDtt correlation debug layers",                        this.corr_mode_debug,
					"false - return (old) per-coord correlations, true - replace them with more pairs correlation (new)");
			gd.addCheckbox    ("Replace CM layer with mixed/new poly one",                        this.mix_corr_poly);
@@ -467,6 +471,7 @@ public class ImageDttParameters {

	}
	public void dialogAnswers(GenericJTabbedDialog gd) {
			this.gpu_mode_debug =        gd.getNextBoolean();
			this.corr_mode_debug=        gd.getNextBoolean();
			this.mix_corr_poly=          gd.getNextBoolean();
			this.min_poly_strength=      gd.getNextNumber();
@@ -608,6 +613,7 @@ public class ImageDttParameters {


	public void setProperties(String prefix,Properties properties){
		properties.setProperty(prefix+"gpu_mode_debug",       this.gpu_mode_debug+"");
		properties.setProperty(prefix+"corr_mode_debug",      this.corr_mode_debug+"");
		properties.setProperty(prefix+"mix_corr_poly",        this.mix_corr_poly+"");
		properties.setProperty(prefix+"min_poly_strength",    this.min_poly_strength+"");
@@ -749,6 +755,7 @@ public class ImageDttParameters {
	}

	public void getProperties(String prefix,Properties properties){
		if (properties.getProperty(prefix+"gpu_mode_debug")!=null)        this.gpu_mode_debug=Boolean.parseBoolean(properties.getProperty(prefix+"gpu_mode_debug"));
		if (properties.getProperty(prefix+"corr_mode_debug")!=null)       this.corr_mode_debug=Boolean.parseBoolean(properties.getProperty(prefix+"corr_mode_debug"));
		if (properties.getProperty(prefix+"mix_corr_poly")!=null)         this.mix_corr_poly=Boolean.parseBoolean(properties.getProperty(prefix+"mix_corr_poly"));
		if (properties.getProperty(prefix+"min_poly_strength")!=null)     this.min_poly_strength=Double.parseDouble(properties.getProperty(prefix+"min_poly_strength"));
@@ -890,6 +897,7 @@ public class ImageDttParameters {
	@Override
	public ImageDttParameters clone() throws CloneNotSupportedException {
        ImageDttParameters idp =     new ImageDttParameters();
		idp.gpu_mode_debug =         this.gpu_mode_debug;
		idp.corr_mode_debug =        this.corr_mode_debug;
		idp.mix_corr_poly =          this.mix_corr_poly;
		idp.min_poly_strength =      this.min_poly_strength;
Loading