[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [60400] trunk/blender/intern/cycles: Fix #35684: cycles unable to use full 6GB of memory on NVidia Titan GPU.

Brecht Van Lommel brechtvanlommel at pandora.be
Fri Sep 27 21:09:31 CEST 2013


Revision: 60400
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=60400
Author:   blendix
Date:     2013-09-27 19:09:31 +0000 (Fri, 27 Sep 2013)
Log Message:
-----------
Fix #35684: cycles unable to use full 6GB of memory on NVidia Titan GPU. We now
use arrays instead of textures for general storage on this card (image textures
are still stored as texture). Textures were found to be faster on older cards,
but the limits on 1D texture size have not increased along with the memory size,
which meant that the full 6 GB could not be used.

The performance actually seems to be slightly better with arrays in some tests
on Titan. For older cards there seems to be a bit of a mix, some are better and
others not. We may change those to use arrays too, but more testing is needed,
only Titan and Tesla K20 (sm_35) is changed for now.

The fact that arrays are faster is a bit surprising, as others found textures
to be faster on Kepler. However even if they were, the memory limitation is
more important to solve anyway.
https://research.nvidia.com/publication/understanding-efficiency-ray-traversal-gpus-kepler-and-fermi-addendum

Modified Paths:
--------------
    trunk/blender/intern/cycles/device/device_cuda.cpp
    trunk/blender/intern/cycles/kernel/kernel_bvh.h
    trunk/blender/intern/cycles/kernel/kernel_compat_cuda.h
    trunk/blender/intern/cycles/kernel/kernel_globals.h
    trunk/blender/intern/cycles/kernel/kernel_primitive.h

Modified: trunk/blender/intern/cycles/device/device_cuda.cpp
===================================================================
--- trunk/blender/intern/cycles/device/device_cuda.cpp	2013-09-27 18:55:26 UTC (rev 60399)
+++ trunk/blender/intern/cycles/device/device_cuda.cpp	2013-09-27 19:09:31 UTC (rev 60400)
@@ -43,7 +43,9 @@
 	CUmodule cuModule;
 	map<device_ptr, bool> tex_interp_map;
 	int cuDevId;
+	int cuDevArchitecture;
 	bool first_error;
+	bool use_texture_storage;
 
 	struct PixelMem {
 		GLuint cuPBO;
@@ -173,6 +175,7 @@
 	{
 		first_error = true;
 		background = background_;
+		use_texture_storage = true;
 
 		cuDevId = info.num;
 		cuDevice = 0;
@@ -203,6 +206,15 @@
 		if(cuda_error_(result, "cuCtxCreate"))
 			return;
 
+		int major, minor;
+		cuDeviceComputeCapability(&major, &minor, cuDevId);
+		cuDevArchitecture = major*100 + minor*10;
+
+		/* In order to use full 6GB of memory on Titan cards, use arrays instead
+		 * of textures. On earlier cards this seems slower, but on Titan it is
+		 * actually slightly faster in tests. */
+		use_texture_storage = (cuDevArchitecture < 350);
+
 		cuda_pop_context();
 	}
 
@@ -210,8 +222,7 @@
 	{
 		task_pool.stop();
 
-		cuda_push_context();
-		cuda_assert(cuCtxDetach(cuContext))
+		cuda_assert(cuCtxDestroy(cuContext))
 	}
 
 	bool support_device(bool experimental)
@@ -448,91 +459,119 @@
 		CUarray_format_enum format;
 		size_t dsize = datatype_size(mem.data_type);
 		size_t size = mem.memory_size();
+		bool use_texture = interpolation || use_texture_storage;
 
-		switch(mem.data_type) {
-			case TYPE_UCHAR: format = CU_AD_FORMAT_UNSIGNED_INT8; break;
-			case TYPE_UINT: format = CU_AD_FORMAT_UNSIGNED_INT32; break;
-			case TYPE_INT: format = CU_AD_FORMAT_SIGNED_INT32; break;
-			case TYPE_FLOAT: format = CU_AD_FORMAT_FLOAT; break;
-			default: assert(0); return;
-		}
+		if(use_texture) {
 
-		CUtexref texref = NULL;
+			switch(mem.data_type) {
+				case TYPE_UCHAR: format = CU_AD_FORMAT_UNSIGNED_INT8; break;
+				case TYPE_UINT: format = CU_AD_FORMAT_UNSIGNED_INT32; break;
+				case TYPE_INT: format = CU_AD_FORMAT_SIGNED_INT32; break;
+				case TYPE_FLOAT: format = CU_AD_FORMAT_FLOAT; break;
+				default: assert(0); return;
+			}
 
-		cuda_push_context();
-		cuda_assert(cuModuleGetTexRef(&texref, cuModule, name))
+			CUtexref texref = NULL;
 
-		if(!texref) {
-			cuda_pop_context();
-			return;
-		}
+			cuda_push_context();
+			cuda_assert(cuModuleGetTexRef(&texref, cuModule, name))
 
-		if(interpolation) {
-			CUarray handle = NULL;
-			CUDA_ARRAY_DESCRIPTOR desc;
-
-			desc.Width = mem.data_width;
-			desc.Height = mem.data_height;
-			desc.Format = format;
-			desc.NumChannels = mem.data_elements;
-
-			cuda_assert(cuArrayCreate(&handle, &desc))
-
-			if(!handle) {
+			if(!texref) {
 				cuda_pop_context();
 				return;
 			}
 
-			if(mem.data_height > 1) {
-				CUDA_MEMCPY2D param;
-				memset(&param, 0, sizeof(param));
-				param.dstMemoryType = CU_MEMORYTYPE_ARRAY;
-				param.dstArray = handle;
-				param.srcMemoryType = CU_MEMORYTYPE_HOST;
-				param.srcHost = (void*)mem.data_pointer;
-				param.srcPitch = mem.data_width*dsize*mem.data_elements;
-				param.WidthInBytes = param.srcPitch;
-				param.Height = mem.data_height;
+			if(interpolation) {
+				CUarray handle = NULL;
+				CUDA_ARRAY_DESCRIPTOR desc;
 
-				cuda_assert(cuMemcpy2D(&param))
+				desc.Width = mem.data_width;
+				desc.Height = mem.data_height;
+				desc.Format = format;
+				desc.NumChannels = mem.data_elements;
+
+				cuda_assert(cuArrayCreate(&handle, &desc))
+
+				if(!handle) {
+					cuda_pop_context();
+					return;
+				}
+
+				if(mem.data_height > 1) {
+					CUDA_MEMCPY2D param;
+					memset(&param, 0, sizeof(param));
+					param.dstMemoryType = CU_MEMORYTYPE_ARRAY;
+					param.dstArray = handle;
+					param.srcMemoryType = CU_MEMORYTYPE_HOST;
+					param.srcHost = (void*)mem.data_pointer;
+					param.srcPitch = mem.data_width*dsize*mem.data_elements;
+					param.WidthInBytes = param.srcPitch;
+					param.Height = mem.data_height;
+
+					cuda_assert(cuMemcpy2D(&param))
+				}
+				else
+					cuda_assert(cuMemcpyHtoA(handle, 0, (void*)mem.data_pointer, size))
+
+				cuda_assert(cuTexRefSetArray(texref, handle, CU_TRSA_OVERRIDE_FORMAT))
+
+				cuda_assert(cuTexRefSetFilterMode(texref, CU_TR_FILTER_MODE_LINEAR))
+				cuda_assert(cuTexRefSetFlags(texref, CU_TRSF_NORMALIZED_COORDINATES))
+
+				mem.device_pointer = (device_ptr)handle;
+
+				stats.mem_alloc(size);
 			}
-			else
-				cuda_assert(cuMemcpyHtoA(handle, 0, (void*)mem.data_pointer, size))
+			else {
+				cuda_pop_context();
 
-			cuda_assert(cuTexRefSetArray(texref, handle, CU_TRSA_OVERRIDE_FORMAT))
+				mem_alloc(mem, MEM_READ_ONLY);
+				mem_copy_to(mem);
 
-			cuda_assert(cuTexRefSetFilterMode(texref, CU_TR_FILTER_MODE_LINEAR))
-			cuda_assert(cuTexRefSetFlags(texref, CU_TRSF_NORMALIZED_COORDINATES))
+				cuda_push_context();
 
-			mem.device_pointer = (device_ptr)handle;
+				cuda_assert(cuTexRefSetAddress(NULL, texref, cuda_device_ptr(mem.device_pointer), size))
+				cuda_assert(cuTexRefSetFilterMode(texref, CU_TR_FILTER_MODE_POINT))
+				cuda_assert(cuTexRefSetFlags(texref, CU_TRSF_READ_AS_INTEGER))
+			}
 
-			stats.mem_alloc(size);
+			if(periodic) {
+				cuda_assert(cuTexRefSetAddressMode(texref, 0, CU_TR_ADDRESS_MODE_WRAP))
+				cuda_assert(cuTexRefSetAddressMode(texref, 1, CU_TR_ADDRESS_MODE_WRAP))
+			}
+			else {
+				cuda_assert(cuTexRefSetAddressMode(texref, 0, CU_TR_ADDRESS_MODE_CLAMP))
+				cuda_assert(cuTexRefSetAddressMode(texref, 1, CU_TR_ADDRESS_MODE_CLAMP))
+			}
+			cuda_assert(cuTexRefSetFormat(texref, format, mem.data_elements))
+
+			cuda_pop_context();
 		}
 		else {
-			cuda_pop_context();
-
 			mem_alloc(mem, MEM_READ_ONLY);
 			mem_copy_to(mem);
 
 			cuda_push_context();
 
-			cuda_assert(cuTexRefSetAddress(NULL, texref, cuda_device_ptr(mem.device_pointer), size))
-			cuda_assert(cuTexRefSetFilterMode(texref, CU_TR_FILTER_MODE_POINT))
-			cuda_assert(cuTexRefSetFlags(texref, CU_TRSF_READ_AS_INTEGER))
-		}
+			CUdeviceptr cumem;
+			size_t cubytes;
 
-		if(periodic) {
-			cuda_assert(cuTexRefSetAddressMode(texref, 0, CU_TR_ADDRESS_MODE_WRAP))
-			cuda_assert(cuTexRefSetAddressMode(texref, 1, CU_TR_ADDRESS_MODE_WRAP))
+			cuda_assert(cuModuleGetGlobal(&cumem, &cubytes, cuModule, name))
+
+			if(cubytes == 8) {
+				/* 64 bit device pointer */
+				uint64_t ptr = mem.device_pointer;
+				cuda_assert(cuMemcpyHtoD(cumem, (void*)&ptr, cubytes))
+			}
+			else {
+				/* 32 bit device pointer */
+				uint32_t ptr = (uint32_t)mem.device_pointer;
+				cuda_assert(cuMemcpyHtoD(cumem, (void*)&ptr, cubytes))
+			}
+
+			cuda_pop_context();
 		}
-		else {
-			cuda_assert(cuTexRefSetAddressMode(texref, 0, CU_TR_ADDRESS_MODE_CLAMP))
-			cuda_assert(cuTexRefSetAddressMode(texref, 1, CU_TR_ADDRESS_MODE_CLAMP))
-		}
-		cuda_assert(cuTexRefSetFormat(texref, format, mem.data_elements))
 
-		cuda_pop_context();
-
 		tex_interp_map[mem.device_pointer] = interpolation;
 	}
 

Modified: trunk/blender/intern/cycles/kernel/kernel_bvh.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel_bvh.h	2013-09-27 18:55:26 UTC (rev 60399)
+++ trunk/blender/intern/cycles/kernel/kernel_bvh.h	2013-09-27 19:09:31 UTC (rev 60400)
@@ -809,11 +809,16 @@
 #include "kernel_bvh_subsurface.h"
 #endif
 
-
+/* to work around titan bug when using arrays instead of textures */
+#if !defined(__KERNEL_CUDA__) || defined(__KERNEL_CUDA_TEX_STORAGE__)
+__device_inline
+#else
+__device_noinline
+#endif
 #ifdef __HAIR__ 
-__device_inline bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect, uint *lcg_state, float difl, float extmax)
+bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect, uint *lcg_state, float difl, float extmax)
 #else
-__device_inline bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect)
+bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect)
 #endif
 {
 #ifdef __OBJECT_MOTION__
@@ -851,8 +856,14 @@
 #endif /* __KERNEL_CPU__ */
 }
 
+/* to work around titan bug when using arrays instead of textures */
 #ifdef __SUBSURFACE__
-__device_inline uint scene_intersect_subsurface(KernelGlobals *kg, const Ray *ray, Intersection *isect, int subsurface_object, uint *lcg_state, int max_hits)
+#if !defined(__KERNEL_CUDA__) || defined(__KERNEL_CUDA_TEX_STORAGE__)
+__device_inline
+#else
+__device_noinline
+#endif
+uint scene_intersect_subsurface(KernelGlobals *kg, const Ray *ray, Intersection *isect, int subsurface_object, uint *lcg_state, int max_hits)
 {
 #ifdef __OBJECT_MOTION__
 	if(kernel_data.bvh.have_motion) {

Modified: trunk/blender/intern/cycles/kernel/kernel_compat_cuda.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel_compat_cuda.h	2013-09-27 18:55:26 UTC (rev 60399)
+++ trunk/blender/intern/cycles/kernel/kernel_compat_cuda.h	2013-09-27 19:09:31 UTC (rev 60400)
@@ -57,7 +57,18 @@
 
 /* Macros to handle different memory storage on different devices */
 
+/* In order to use full 6GB of memory on Titan cards, use arrays instead
+ * of textures. On earlier cards this seems slower, but on Titan it is
+ * actually slightly faster in tests. */
+#if __CUDA_ARCH__ < 350
+#define __KERNEL_CUDA_TEX_STORAGE__
+#endif
+
+#ifdef __KERNEL_CUDA_TEX_STORAGE__
 #define kernel_tex_fetch(t, index) tex1Dfetch(t, index)
+#else
+#define kernel_tex_fetch(t, index) t[(index)]
+#endif
 #define kernel_tex_image_interp(t, x, y) tex2D(t, x, y)
 
 #define kernel_data __data

Modified: trunk/blender/intern/cycles/kernel/kernel_globals.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel_globals.h	2013-09-27 18:55:26 UTC (rev 60399)
+++ trunk/blender/intern/cycles/kernel/kernel_globals.h	2013-09-27 19:09:31 UTC (rev 60400)
@@ -66,7 +66,11 @@
 __constant__ KernelData __data;
 typedef struct KernelGlobals {} KernelGlobals;
 
+#ifdef __KERNEL_CUDA_TEX_STORAGE__
 #define KERNEL_TEX(type, ttype, name) ttype name;
+#else
+#define KERNEL_TEX(type, ttype, name) const __constant__ __device__ type *name;
+#endif
 #define KERNEL_IMAGE_TEX(type, ttype, name) ttype name;
 #include "kernel_textures.h"
 

Modified: trunk/blender/intern/cycles/kernel/kernel_primitive.h
===================================================================

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list