Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^7: Rosetta Code: Long List is Long (faster - vec - OpenMP)

by eyepopslikeamosquito (Archbishop)
on Jan 11, 2023 at 20:59 UTC ( [id://11149540]=note: print w/replies, xml ) Need Help??


in reply to Re^6: Rosetta Code: Long List is Long (faster - vec - OpenMP)
in thread Rosetta Code: Long List is Long

Very interesting! This will help me get started with OpenMP, which I've been meaning to do for a while now.

Unfortunately, strtok is not thread-safe e.g. strtok(NULL, "\n") causing segfault. So I factored out strtok.

Ha ha, that I used strtok at all is an indication of how desperate I was, given I singled this function out for a special dishonourable mention at On Interfaces and APIs. :)

To round out this section, notice that the ANSI C strtok function has a shocker of an interface: it surprises by writing NULLs into the input string being tokenized; the first call has different semantics to subsequent calls (distinguished by special-case logic on the value of its first parameter); and it stores state between calls so that only one sequence of calls can be active at a time (bad enough in a single-threaded environment, but so intolerable in multi-threaded and signal handler (reentrant) environments that the POSIX threads committee invented a replacement strtok_r function).

Replies are listed 'Best First'.
Re^8: Rosetta Code: Long List is Long (faster - vec - OpenMP)
by marioroy (Prior) on Jan 11, 2023 at 22:07 UTC

    It was weird experiencing the segfault due to strtok(NULL, "\n").

    Great! What is strtok_r() function in C language? I compared strtok_r with context against strchr. After testing, I updated the OpenMP demonstration to find the tab character within the string. Now passing: limited length and variable length words, including OpenMP. Update 2 enables parallel sort.

    # 15 input files, 1 thread, clang++ -std=c++20 strtok_r 9.655 seconds strchr 8.713 seconds
    while ( ::fgets(line, MAX_LINE_LEN_L, fh) != NULL ) { found = ::strchr(line, '\t'); count = ::atoll( &line[found - line + 1] ); line[found - line] = '\0'; // word ... }

    Also, I replaced strlen(...) with found - line.

    ::memcpy( fixword.data(), line, found - line );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found