Revision: 42642
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42642
Author:   jbakker
Date:     2011-12-15 12:42:43 +0000 (Thu, 15 Dec 2011)
Log Message:
-----------
Tile branch:
added the worker pattern in the workscheduler for stability reasons.
the execution time is slower (+-70% of normal speed) but it does not crash that 
much.

Goal is still to use the pthreaded model. but this needs some more complex 
debugging.

Jeroen

Modified Paths:
--------------
    branches/tile/source/blender/compositor/COM_defines.h
    branches/tile/source/blender/compositor/intern/COM_ExecutionGroup.cpp
    branches/tile/source/blender/compositor/intern/COM_MemoryManagerState.cpp
    branches/tile/source/blender/compositor/intern/COM_WorkPackage.cpp
    branches/tile/source/blender/compositor/intern/COM_WorkScheduler.cpp
    branches/tile/source/blender/compositor/intern/COM_compositor.cpp
    
branches/tile/source/blender/compositor/operations/COM_CompositorOperation.cpp
    
branches/tile/source/blender/compositor/operations/COM_RenderLayersBaseProg.h

Modified: branches/tile/source/blender/compositor/COM_defines.h
===================================================================
--- branches/tile/source/blender/compositor/COM_defines.h       2011-12-15 
12:18:09 UTC (rev 42641)
+++ branches/tile/source/blender/compositor/COM_defines.h       2011-12-15 
12:42:43 UTC (rev 42642)
@@ -70,9 +70,15 @@
 #define COM_TM_NOTHREAD 2
 
 /**
+  * COM_TM_WORKER is a multithreaded model, which uses the BLI_Worker pattern. 
it is slower as it searched for free threads.
+  * But for stability reasons you can select this one.
+  */
+#define COM_TM_WORKER 3
+
+/**
   * COM_CURRENT_THREADING_MODEL can be one of the above, COM_PTHREAD is 
currently default.
   */
-#define COM_CURRENT_THREADING_MODEL COM_TM_PTHREAD
+#define COM_CURRENT_THREADING_MODEL COM_TM_WORKER
 
 
 // chunk order

Modified: branches/tile/source/blender/compositor/intern/COM_ExecutionGroup.cpp
===================================================================
--- branches/tile/source/blender/compositor/intern/COM_ExecutionGroup.cpp       
2011-12-15 12:18:09 UTC (rev 42641)
+++ branches/tile/source/blender/compositor/intern/COM_ExecutionGroup.cpp       
2011-12-15 12:42:43 UTC (rev 42642)
@@ -95,6 +95,7 @@
                        this->openCL = operation->isOpenCL();
                        this->initialized = true;
                }
+               printf("Adding operation to group %d, %d\n", this, operation);
                this->operations.push_back(operation);
                if (operation->isReadBufferOperation()) {
                        ReadBufferOperation* readOperation = 
(ReadBufferOperation*)operation;
@@ -414,7 +415,11 @@
 }
 
 void ExecutionGroup::finalizeChunkExecution(int chunkNumber, MemoryBuffer** 
memoryBuffers) {
-       this->chunkExecutionStates[chunkNumber] = COM_ES_EXECUTED;
+       if (this->chunkExecutionStates[chunkNumber] == COM_ES_SCHEDULED)
+               this->chunkExecutionStates[chunkNumber] = COM_ES_EXECUTED;
+       else 
+               throw "Threading inconsistency";
+       
        this->chunksFinished++;
        if (memoryBuffers) {
                for (unsigned int index = 0 ; index < 
this->cachedMaxReadBufferOffset; index ++) {
@@ -487,9 +492,12 @@
 }
 
 bool ExecutionGroup::scheduleChunk(unsigned int chunkNumber) {
-       this->chunkExecutionStates[chunkNumber] = COM_ES_SCHEDULED;
-       WorkScheduler::schedule(this, chunkNumber);
-       return true;
+       if (this->chunkExecutionStates[chunkNumber] == COM_ES_NOT_SCHEDULED) {
+               this->chunkExecutionStates[chunkNumber] = COM_ES_SCHEDULED;
+               WorkScheduler::schedule(this, chunkNumber);
+               return true;
+       }
+       return false;
 }
 
 bool ExecutionGroup::scheduleChunkWhenPossible(ExecutionSystem * graph, int 
xChunk, int yChunk) {

Modified: 
branches/tile/source/blender/compositor/intern/COM_MemoryManagerState.cpp
===================================================================
--- branches/tile/source/blender/compositor/intern/COM_MemoryManagerState.cpp   
2011-12-15 12:18:09 UTC (rev 42641)
+++ branches/tile/source/blender/compositor/intern/COM_MemoryManagerState.cpp   
2011-12-15 12:42:43 UTC (rev 42642)
@@ -67,7 +67,11 @@
         if (oldbuffer) delete oldbuffer;
     }
 
-       this->chunkBuffers[chunkNumber] = buffer;
+       if (this->chunkBuffers[chunkNumber] == NULL) {
+               this->chunkBuffers[chunkNumber] = buffer;
+       } else {
+               throw "ALREADY ALLOCATED!";
+       }
     BLI_mutex_unlock(&this->mutex);
 }
 

Modified: branches/tile/source/blender/compositor/intern/COM_WorkPackage.cpp
===================================================================
--- branches/tile/source/blender/compositor/intern/COM_WorkPackage.cpp  
2011-12-15 12:18:09 UTC (rev 42641)
+++ branches/tile/source/blender/compositor/intern/COM_WorkPackage.cpp  
2011-12-15 12:42:43 UTC (rev 42642)
@@ -21,10 +21,8 @@
  */
 
 #include "COM_WorkPackage.h"
-#include <stdio.h>
 
 WorkPackage::WorkPackage(ExecutionGroup *group, unsigned int chunkNumber) {
-       printf("%d: %d\n", group, chunkNumber);
-    this->executionGroup = group;
+       this->executionGroup = group;
        this->chunkNumber = chunkNumber;
 }

Modified: branches/tile/source/blender/compositor/intern/COM_WorkScheduler.cpp
===================================================================
--- branches/tile/source/blender/compositor/intern/COM_WorkScheduler.cpp        
2011-12-15 12:18:09 UTC (rev 42641)
+++ branches/tile/source/blender/compositor/intern/COM_WorkScheduler.cpp        
2011-12-15 12:42:43 UTC (rev 42642)
@@ -28,11 +28,11 @@
 #include "COM_OpenCLDevice.h"
 #include "OCL_opencl.h"
 #include "stdio.h"
-#include "MEM_guardedalloc.h"
 #include "COM_OpenCLKernels.cl.cpp"
 
 #if COM_CURRENT_THREADING_MODEL == COM_TM_PTHREAD
 #elif COM_CURRENT_THREADING_MODEL == COM_TM_NOTHREAD
+#elif COM_CURRENT_THREADING_MODEL == COM_TM_WORKER
 #else
 #error WorkScheduler: No threading model configured
 #endif
@@ -60,11 +60,23 @@
 static ListBase gputhreads;
 #endif
 
+#if COM_CURRENT_THREADING_MODEL == COM_TM_WORKER
+ThreadedWorker *cpuworker;
+#endif
+
 #if COM_OPENCL_ENABLED
 static cl_context context;
 static cl_program program;
 #endif
 
+#if COM_CURRENT_THREADING_MODEL == COM_TM_WORKER
+void* worker_execute_cpu(void* data) {
+       CPUDevice device;
+       WorkPackage * package = (WorkPackage*)data;
+       device.execute(package);
+       delete package;
+}
+#endif
 #if COM_CURRENT_THREADING_MODEL == COM_TM_PTHREAD
 void* WorkScheduler::thread_execute_cpu(void* data) {
        bool continueLoop = true;
@@ -145,6 +157,8 @@
        CPUDevice device;
        device.execute(package);
        delete package;
+#elif COM_CURRENT_THREADING_MODEL == COM_TM_WORKER
+       BLI_insert_work(cpuworker, package);
 #endif
 }
 
@@ -167,6 +181,9 @@
                openclActive = false;
        }
 #endif
+#if COM_CURRENT_THREADING_MODEL == COM_TM_WORKER
+       cpuworker = BLI_create_worker(worker_execute_cpu, cpudevices.size(), 0);
+#endif
        state = COM_WSS_STARTED;
 }
 
@@ -176,11 +193,14 @@
        BLI_end_threads(&cputhreads);
        BLI_end_threads(&gputhreads);
 #endif
+#if COM_CURRENT_THREADING_MODEL == COM_TM_WORKER
+       BLI_destroy_worker(cpuworker);
+#endif
        state = COM_WSS_STOPPED;
 }
 
 void WorkScheduler::finish() {
-       while ((cpuwork.size() + gpuwork.size()) >0) {
+       while (!cpuwork.empty() && !gpuwork.empty()) {
                PIL_sleep_ms(10);
        }
 }
@@ -206,6 +226,14 @@
                device->initialize();
                cpudevices.push_back(device);
        }
+#elif COM_CURRENT_THREADING_MODEL == COM_TM_WORKER
+       int numberOfCPUThreads = BLI_system_thread_count();
+
+       for (int index = 0 ; index < numberOfCPUThreads ; index ++) {
+               CPUDevice *device = new CPUDevice();
+               device->initialize();
+               cpudevices.push_back(device);
+       }
 #endif
 
 #if COM_OPENCL_ENABLED

Modified: branches/tile/source/blender/compositor/intern/COM_compositor.cpp
===================================================================
--- branches/tile/source/blender/compositor/intern/COM_compositor.cpp   
2011-12-15 12:18:09 UTC (rev 42641)
+++ branches/tile/source/blender/compositor/intern/COM_compositor.cpp   
2011-12-15 12:42:43 UTC (rev 42642)
@@ -30,18 +30,18 @@
 #include "COM_ExecutionSystem.h"
 #include "COM_WorkScheduler.h"
 
-static ThreadMutex *mutex=NULL;
+static ThreadMutex *compositorMutex;
 void COM_execute(bNodeTree *editingtree, int rendering) {
-       if (mutex == NULL) { /// TODO: move to blender startup phase
-               mutex = new ThreadMutex();
-               BLI_mutex_init(mutex);
+       if (compositorMutex == NULL) { /// TODO: move to blender startup phase
+               compositorMutex = new ThreadMutex();
+               BLI_mutex_init(compositorMutex);
                WorkScheduler::initialize(); ///TODO: call 
workscheduler.deinitialize somewhere
        }
-       BLI_mutex_lock(mutex);
+       BLI_mutex_lock(compositorMutex);
        if (editingtree->test_break && 
editingtree->test_break(editingtree->tbh)) {
                // during editing multiple calls to this method can be 
triggered.
                // make sure one the last one will be doing the work.
-               BLI_mutex_unlock(mutex);
+               BLI_mutex_unlock(compositorMutex);
                return;
 
        }
@@ -55,5 +55,5 @@
        system->execute();
        delete system;
 
-       BLI_mutex_unlock(mutex);
+       BLI_mutex_unlock(compositorMutex);
 }

Modified: 
branches/tile/source/blender/compositor/operations/COM_CompositorOperation.cpp
===================================================================
--- 
branches/tile/source/blender/compositor/operations/COM_CompositorOperation.cpp  
    2011-12-15 12:18:09 UTC (rev 42641)
+++ 
branches/tile/source/blender/compositor/operations/COM_CompositorOperation.cpp  
    2011-12-15 12:42:43 UTC (rev 42642)
@@ -51,7 +51,7 @@
        this->imageInput = getInputSocketReader(0);
        this->alphaInput = getInputSocketReader(1);
        if (this->getWidth() * this->getHeight() != 0) {
-               this->outputBuffer=(float*) 
MEM_mapallocN(this->getWidth()*this->getHeight()*4*sizeof(float), 
"CompositorOperation");
+               this->outputBuffer=(float*) 
MEM_callocN(this->getWidth()*this->getHeight()*4*sizeof(float), 
"CompositorOperation");
        }
        const Scene * scene = this->scene;
        Render* re= RE_GetRender(scene->id.name);
@@ -77,6 +77,7 @@
 
 
 void CompositorOperation::executeRegion(rcti *rect, unsigned int tileNumber, 
MemoryBuffer** memoryBuffers) {
+       float color[8]; // 7 is enough
        float* buffer = this->outputBuffer;
 
        if (!buffer) return;
@@ -91,17 +92,19 @@
 
        for (y = y1 ; y < y2 && (!breaked); y++) {
                for (x = x1 ; x < x2 && (!breaked) ; x++) {
-                       imageInput->read(&(buffer[offset]), x, y, 
memoryBuffers);
+                       imageInput->read(color, x, y, memoryBuffers);
                        if (alphaInput != NULL) {
-                               alphaInput->read(&(buffer[offset+3]), x, y, 
memoryBuffers);
+                               alphaInput->read(&(color[3]), x, y, 
memoryBuffers);
                        }
+                       buffer[offset] = color[0];
+                       buffer[offset+1] = color[1];
+                       buffer[offset+2] = color[2];
+                       buffer[offset+3] = color[3];
                        offset +=4;
                        if (tree->test_break && tree->test_break(tree->tbh)) {
                                breaked = true;
                        }
-
                }
                offset += (this->getWidth()-(x2-x1))*4;
        }
 }
-

Modified: 
branches/tile/source/blender/compositor/operations/COM_RenderLayersBaseProg.h
===================================================================
--- 
branches/tile/source/blender/compositor/operations/COM_RenderLayersBaseProg.h   
    2011-12-15 12:18:09 UTC (rev 42641)
+++ 
branches/tile/source/blender/compositor/operations/COM_RenderLayersBaseProg.h   
    2011-12-15 12:42:43 UTC (rev 42642)
@@ -63,7 +63,7 @@
     int renderpass;
 
        int elementsize;
-
+       
 protected:
     /**
       * Constructor

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to