[Bf-blender-cvs] [0d5cef98980] master: Cleanup: removed unnecessary allocations.

Jeroen Bakker noreply at git.blender.org
Wed Mar 24 14:30:26 CET 2021


Commit: 0d5cef989803d047696b5f76f0cc95a8cc07a25a
Author: Jeroen Bakker
Date:   Wed Mar 24 12:53:41 2021 +0100
Branches: master
https://developer.blender.org/rB0d5cef989803d047696b5f76f0cc95a8cc07a25a

Cleanup: removed unnecessary allocations.

===================================================================

M	source/blender/compositor/intern/COM_CPUDevice.h
M	source/blender/compositor/intern/COM_Device.h
M	source/blender/compositor/intern/COM_OpenCLDevice.cc
M	source/blender/compositor/intern/COM_OpenCLDevice.h
M	source/blender/compositor/intern/COM_WorkScheduler.cc

===================================================================

diff --git a/source/blender/compositor/intern/COM_CPUDevice.h b/source/blender/compositor/intern/COM_CPUDevice.h
index 962380d7bc8..6df1f41419d 100644
--- a/source/blender/compositor/intern/COM_CPUDevice.h
+++ b/source/blender/compositor/intern/COM_CPUDevice.h
@@ -33,7 +33,7 @@ class CPUDevice : public Device {
    * \brief execute a WorkPackage
    * \param work: the WorkPackage to execute
    */
-  void execute(WorkPackage *work);
+  void execute(WorkPackage *work) override;
 
   int thread_id()
   {
diff --git a/source/blender/compositor/intern/COM_Device.h b/source/blender/compositor/intern/COM_Device.h
index 0b0f0f5c1c6..0a456760045 100644
--- a/source/blender/compositor/intern/COM_Device.h
+++ b/source/blender/compositor/intern/COM_Device.h
@@ -36,21 +36,6 @@ class Device {
   {
   }
 
-  /**
-   * \brief initialize the device
-   */
-  virtual bool initialize()
-  {
-    return true;
-  }
-
-  /**
-   * \brief deinitialize the device
-   */
-  virtual void deinitialize()
-  {
-  }
-
   /**
    * \brief execute a WorkPackage
    * \param work: the WorkPackage to execute
diff --git a/source/blender/compositor/intern/COM_OpenCLDevice.cc b/source/blender/compositor/intern/COM_OpenCLDevice.cc
index 9a6012e5c68..4ac6bd50380 100644
--- a/source/blender/compositor/intern/COM_OpenCLDevice.cc
+++ b/source/blender/compositor/intern/COM_OpenCLDevice.cc
@@ -43,16 +43,12 @@ OpenCLDevice::OpenCLDevice(cl_context context,
   this->m_program = program;
   this->m_queue = nullptr;
   this->m_vendorID = vendorId;
-}
 
-bool OpenCLDevice::initialize()
-{
   cl_int error;
   this->m_queue = clCreateCommandQueue(this->m_context, this->m_device, 0, &error);
-  return false;
 }
 
-void OpenCLDevice::deinitialize()
+OpenCLDevice::~OpenCLDevice()
 {
   if (this->m_queue) {
     clReleaseCommandQueue(this->m_queue);
diff --git a/source/blender/compositor/intern/COM_OpenCLDevice.h b/source/blender/compositor/intern/COM_OpenCLDevice.h
index e4fd397b4e8..30d9a59d182 100644
--- a/source/blender/compositor/intern/COM_OpenCLDevice.h
+++ b/source/blender/compositor/intern/COM_OpenCLDevice.h
@@ -65,26 +65,13 @@ class OpenCLDevice : public Device {
    * \param vendorID:
    */
   OpenCLDevice(cl_context context, cl_device_id device, cl_program program, cl_int vendorId);
-
-  /**
-   * \brief initialize the device
-   * During initialization the OpenCL cl_command_queue is created
-   * the command queue is stored in the field queue.
-   * \see queue
-   */
-  bool initialize();
-
-  /**
-   * \brief de-initialize the device
-   * During de-initialization the command queue is cleared
-   */
-  void deinitialize();
+  ~OpenCLDevice();
 
   /**
    * \brief execute a WorkPackage
    * \param work: the WorkPackage to execute
    */
-  void execute(WorkPackage *work);
+  void execute(WorkPackage *work) override;
 
   /**
    * \brief determine an image format
diff --git a/source/blender/compositor/intern/COM_WorkScheduler.cc b/source/blender/compositor/intern/COM_WorkScheduler.cc
index 56956b9d097..7d9dd502762 100644
--- a/source/blender/compositor/intern/COM_WorkScheduler.cc
+++ b/source/blender/compositor/intern/COM_WorkScheduler.cc
@@ -70,7 +70,7 @@ static struct {
     /** \brief list of all CPUDevices. for every hardware thread an instance of CPUDevice is
      * created
      */
-    blender::Vector<CPUDevice *> devices;
+    blender::Vector<CPUDevice> devices;
 
     /** \brief list of all thread for every CPUDevice in cpudevices a thread exists. */
     ListBase threads;
@@ -89,7 +89,7 @@ static struct {
     cl_program program;
     /** \brief list of all OpenCLDevices. for every OpenCL GPU device an instance of OpenCLDevice
      * is created. */
-    blender::Vector<OpenCLDevice *> devices;
+    blender::Vector<OpenCLDevice> devices;
     /** \brief list of all thread for every GPUDevice in cpudevices a thread exists. */
     ListBase threads;
     /** \brief all scheduled work for the GPU. */
@@ -130,9 +130,8 @@ static void opencl_start(CompositorContext &context)
     BLI_threadpool_init(&g_work_scheduler.opencl.threads,
                         thread_execute_gpu,
                         g_work_scheduler.opencl.devices.size());
-    for (int index = 0; index < g_work_scheduler.opencl.devices.size(); index++) {
-      Device *device = g_work_scheduler.opencl.devices[index];
-      BLI_threadpool_insert(&g_work_scheduler.opencl.threads, device);
+    for (Device &device : g_work_scheduler.opencl.devices) {
+      BLI_threadpool_insert(&g_work_scheduler.opencl.threads, &device);
     }
     g_work_scheduler.opencl.active = true;
   }
@@ -263,12 +262,10 @@ static void opencl_initialize(const bool use_opencl)
             if (error2 != CL_SUCCESS) {
               printf("CLERROR[%d]: %s\n", error2, clewErrorString(error2));
             }
-            OpenCLDevice *clDevice = new OpenCLDevice(g_work_scheduler.opencl.context,
-                                                      device,
-                                                      g_work_scheduler.opencl.program,
-                                                      vendorID);
-            clDevice->initialize();
-            g_work_scheduler.opencl.devices.append(clDevice);
+            g_work_scheduler.opencl.devices.append(OpenCLDevice(g_work_scheduler.opencl.context,
+                                                                device,
+                                                                g_work_scheduler.opencl.program,
+                                                                vendorID));
           }
         }
         MEM_freeN(cldevices);
@@ -282,26 +279,19 @@ static void opencl_initialize(const bool use_opencl)
 
 static void opencl_deinitialize()
 {
-  /* Deinitialize OpenCL GPU's. */
-  if (g_work_scheduler.opencl.initialized) {
-    while (!g_work_scheduler.opencl.devices.is_empty()) {
-      Device *device = g_work_scheduler.opencl.devices.pop_last();
-      device->deinitialize();
-      delete device;
-    }
-    g_work_scheduler.opencl.devices.clear_and_make_inline();
+  g_work_scheduler.opencl.devices.clear_and_make_inline();
 
-    if (g_work_scheduler.opencl.program) {
-      clReleaseProgram(g_work_scheduler.opencl.program);
-      g_work_scheduler.opencl.program = nullptr;
-    }
-    if (g_work_scheduler.opencl.context) {
-      clReleaseContext(g_work_scheduler.opencl.context);
-      g_work_scheduler.opencl.context = nullptr;
-    }
+  if (g_work_scheduler.opencl.program) {
+    clReleaseProgram(g_work_scheduler.opencl.program);
+    g_work_scheduler.opencl.program = nullptr;
+  }
 
-    g_work_scheduler.opencl.initialized = false;
+  if (g_work_scheduler.opencl.context) {
+    clReleaseContext(g_work_scheduler.opencl.context);
+    g_work_scheduler.opencl.context = nullptr;
   }
+
+  g_work_scheduler.opencl.initialized = false;
 }
 
 /* \} */
@@ -347,9 +337,8 @@ static void threading_model_queue_start()
   BLI_threadpool_init(&g_work_scheduler.queue.threads,
                       threading_model_queue_execute,
                       g_work_scheduler.queue.devices.size());
-  for (int index = 0; index < g_work_scheduler.queue.devices.size(); index++) {
-    Device *device = g_work_scheduler.queue.devices[index];
-    BLI_threadpool_insert(&g_work_scheduler.queue.threads, device);
+  for (Device &device : g_work_scheduler.queue.devices) {
+    BLI_threadpool_insert(&g_work_scheduler.queue.threads, &device);
   }
 }
 
@@ -370,25 +359,17 @@ static void threading_model_queue_initialize(const int num_cpu_threads)
 {
   /* Reinitialize if number of threads doesn't match. */
   if (g_work_scheduler.queue.devices.size() != num_cpu_threads) {
-    Device *device;
-
-    while (!g_work_scheduler.queue.devices.is_empty()) {
-      device = g_work_scheduler.queue.devices.pop_last();
-      device->deinitialize();
-      delete device;
-    }
+    g_work_scheduler.queue.devices.clear();
     if (g_work_scheduler.queue.initialized) {
       BLI_thread_local_delete(g_thread_device);
+      g_work_scheduler.queue.initialized = false;
     }
-    g_work_scheduler.queue.initialized = false;
   }
 
   /* Initialize CPU threads. */
   if (!g_work_scheduler.queue.initialized) {
     for (int index = 0; index < num_cpu_threads; index++) {
-      CPUDevice *device = new CPUDevice(index);
-      device->initialize();
-      g_work_scheduler.queue.devices.append(device);
+      g_work_scheduler.queue.devices.append(CPUDevice(index));
     }
     BLI_thread_local_create(g_thread_device);
     g_work_scheduler.queue.initialized = true;
@@ -398,11 +379,6 @@ static void threading_model_queue_deinitialize()
 {
   /* deinitialize CPU threads */
   if (g_work_scheduler.queue.initialized) {
-    while (!g_work_scheduler.queue.devices.is_empty()) {
-      Device *device = g_work_scheduler.queue.devices.pop_last();
-      device->deinitialize();
-      delete device;
-    }
     g_work_scheduler.queue.devices.clear_and_make_inline();
 
     BLI_thread_local_delete(g_thread_device);



More information about the Bf-blender-cvs mailing list