简体   繁体   中英

why c++ openMP program execute longer

I've problem with understanding how it's possible. I've long textfile (ten thousand lines), i read it to variable text as string. I'd like to split it on 200 parts. I've written this code using openMP directives:

    std::string str[200];

    omp_set_num_threads(200);
    #pragma omp parallel 
    {
        #pragma omp for
        for (int i=0;i<200;i++)
        {
            str[i]= text.substr(i*(text.length()/200),text.length()/200);
        }
    }

and it's execution time is 231059 us

if i write it as sequence

        for (int i=0;i<200;i++)
        {
            str[i]= text.substr(i*(text.length()/200),text.length()/200);
        }

execution time is 215902us

Am I using openMP wrong, or what's happen here

substr causes a memory allocation and a memcpy, and not much else. So instead of 1 thread asking the OS for some ram, you now have N threads asking the OS for some RAM, at the same time. This isn't a great design.

Splitting a workload to be tackled by a thread group makes a lot of sense when the workload is CPU intensive. It makes a no sense at all, when all of those threads are competing for the same shared resource (eg the ram). One thread will simply block all the others until each allocation has been completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM