Jump to content

Thread Questions


SpiderPig
 Share

Go to solution Solved by SpiderPig,

Recommended Posts

16 minutes ago, Josh said:

You may be using a lot of threads but you probably aren't using them at max capacity. If you check CPU usage you'll probably be surprised how little they are being used.

I'm using 8 most of the time.  What do you mean by max capacity?  Does the OS run threads slower than it could or something?  CPU usage was only around 50 - 70%

Link to comment
Share on other sites

Keep in mind that Threads are managed by the operating system and depending on the OS some threads can be blocked or used by other programs. Also if you have a modern cpu you normally have also something called Hyperthreading, which means you normally you can use MaxThreads() * 2.0 in parallel, but this depends on the cpu.

Normally the os threadmanagement is highly optimized, so you could push 100 or 1000 threads at once and the os will handle the execution order by itself. So don't worry so much about the actual CPU usage or amount of threads you push to the cpu.

  • Thanks 1
  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

Originally I was pushing out a few hundred or so threads and it was working fine.  From my tests though it seems it's not the fastest way to deal with things.  So now I'm creating only the threads that can run straight away and am spreading the data evenly among them.  It is faster but it is very complicated.  I had to develop this flowgraph to see what was going on and to make sure it was going to work okay.  The biggest problem is wanting to update the terrain while the previous update is still processing.  Then figuring out if even the data that has been processed is even still valid or needs redoing.  I'm nearly there.

VoxelObjectMultithreading.thumb.png.63fbea3200b61dae8e2a15870e25db5e.png

Link to comment
Share on other sites

1 hour ago, klepto2 said:

if you have a modern cpu you normally have also something called Hyperthreading, which means you normally you can use MaxThreads() * 2.0 in parallel, but this depends on the cpu.

MaxThreads returns the number of threads that can be simultaneously run, not the number of CPU cores.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

@Josh I wonder if you would be able to run this small example and see what you make of the memory usage.  I know we talked about the memory usage before but here you can press space to remake all the threads again and every time they run they stack on another 500MB at least in the VS memory monitor.  More noticeable with my voxel terrain as the camera moves around memory goes up.   I don't think it's just debugging info or something as it's the same for debug and release mode.

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

void RunThread() {
    auto r = 0.0f;
    for (int i = 0; i < 1000; i++) {
        r += sqrt(Random(2.0f, 1024.0f));
    }
}

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);
    auto world = CreateWorld();
    auto framebuffer = CreateFramebuffer(window);

    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetFov(70);
    camera->SetPosition(0, 0, -3);

    auto light = CreateDirectionalLight(world);
    light->SetRotation(35, 45, 0);
    light->SetRange(-10, 10);

    auto box = CreateBox(world);
    box->SetColor(0, 0, 1);

    auto actor = CreateActor(box);
    auto component = actor->AddComponent<Mover>();
    component->rotation.y = 45;

    vector<shared_ptr<Thread>> threads;
    threads.reserve(5000);
    for (int id = 0; id < 5000; id++) {
        threads.push_back(CreateThread(RunThread, true));
    }

    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        if (window->KeyHit(KEY_SPACE)) {
            for (int id = 0; id < 5000; id++) {
                threads.push_back(CreateThread(RunThread, true));
            }
        }

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

Link to comment
Share on other sites

You have two memory leaks in your code. You should call threads.clear() when the space key is pressed, and you should clear the events queue or it will just get bigger and bigger:

while (PeekEvent()) WaitEvent();

After fixing those I did find a problem in the thread constructor where another system is getting initialized. I will have a fix for you soon.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

On 1/20/2023 at 12:43 PM, SpiderPig said:

I wonder if you would be able to run this small example and see what you make of the memory usage.

There is also a small memleak in the rendering routine. I'm trying to find it now, but it isn't bad. It works out to about 4 megabytes per hour.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Actually, I am not even sure there's a memory leak at all. Memory usage is incrementing by 10,000 bytes every 8 seconds, but I am using a Vulkan memory management library that could be allocating more memory periodically for some reason...I will look into this further...

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Yeah, there's no mem leak. At first it looks like the memory usage is going up like crazy but eventually it gets to a point where it just stops and stays even, after maybe ten seconds.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...