SpiderPig Posted January 20, 2023 Author Share Posted January 20, 2023 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% Quote Link to comment Share on other sites More sharing options...
Josh Posted January 20, 2023 Share Posted January 20, 2023 If you are at less then 100% there is nothing to worry about. 1 Quote 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 More sharing options...
klepto2 Posted January 20, 2023 Share Posted January 20, 2023 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. 1 Quote Windows 10 Pro 64-Bit-Version NVIDIA Geforce 1080 TI Link to comment Share on other sites More sharing options...
SpiderPig Posted January 20, 2023 Author Share Posted January 20, 2023 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted January 20, 2023 Share Posted January 20, 2023 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. 1 Quote 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 More sharing options...
SpiderPig Posted January 20, 2023 Author Share Posted January 20, 2023 @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; } Quote Link to comment Share on other sites More sharing options...
Josh Posted January 20, 2023 Share Posted January 20, 2023 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. 1 Quote 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 More sharing options...
SpiderPig Posted January 20, 2023 Author Share Posted January 20, 2023 7 hours ago, Josh said: and you should clear the events queue or it will just get bigger and bigger: I thought we only needed that when we used things like the widgets? Are threads emitting events too? Quote Link to comment Share on other sites More sharing options...
Josh Posted January 20, 2023 Share Posted January 20, 2023 If you are using a window, events are constantly being emitted. I am adding a FlushEvents() function in the next build, which will clear the event queue. 1 Quote 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 More sharing options...
SpiderPig Posted January 20, 2023 Author Share Posted January 20, 2023 Ah of course. I only ask because I think there is a lot of examples in the docs that don't look at the event queue in the loop. Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted January 21, 2023 Author Share Posted January 21, 2023 20 hours ago, Josh said: 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. Is this what's causing the memory to grow? I wonder if that's slowing things down a bit too... Quote Link to comment Share on other sites More sharing options...
Josh Posted January 21, 2023 Share Posted January 21, 2023 Yes, and CreateThread will likely be faster as well, although it should not really matter because continuously creating a lot of threads is bad design. 1 Quote 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 More sharing options...
SpiderPig Posted January 21, 2023 Author Share Posted January 21, 2023 Awesome. Yes, I was doing that at first and it was very easy to get things going without any problems but by only creating a few threads and spreading the work among them just feel right, and a bit faster too. Quote Link to comment Share on other sites More sharing options...
Josh Posted January 21, 2023 Share Posted January 21, 2023 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. Quote 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 More sharing options...
Josh Posted January 21, 2023 Share Posted January 21, 2023 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... Quote 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 More sharing options...
Josh Posted January 21, 2023 Share Posted January 21, 2023 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. 1 Quote 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 More sharing options...
SpiderPig Posted January 21, 2023 Author Share Posted January 21, 2023 Ok cool. Will try out the update very soon. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.