Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verilator 4.00 multi-threading on OS X #1332

Closed
veripoolbot opened this issue Aug 25, 2018 · 8 comments
Closed

Verilator 4.00 multi-threading on OS X #1332

veripoolbot opened this issue Aug 25, 2018 · 8 comments

Comments

@veripoolbot
Copy link
Contributor

@veripoolbot veripoolbot commented Aug 25, 2018


Author Name: Florian Zaruba (@zarubaf)
Original Redmine Issue: 1332 from https://www.veripool.org


Thanks for your awesome work on Verilator and related tools.

I just wanted to let you know I am running into issues when translating my model with verilator 4.00 and multi-threading enabled. Apparently on OS X

#include <sched.h>  // For sched_getcpu()
</code>

is not available.

Thanks again,
Florian

@veripoolbot
Copy link
Contributor Author

@veripoolbot veripoolbot commented Aug 25, 2018


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2018-08-25T18:10:29Z


Thanks for trying this out!

I'd really like OSX to work out of the box, so made this high priority.

I don't have access to OS X, would you mind suggesting an alternative function to get the CPU number a process is running on? Then replace using that and see if the rest works. With that I can wrap the function in a OS specific ifdef.

This may help... https://stackoverflow.com/questions/33745364/sched-getcpu-equivalent-for-os-x

@veripoolbot
Copy link
Contributor Author

@veripoolbot veripoolbot commented Aug 25, 2018


Original Redmine Comment
Author Name: Florian Zaruba (@zarubaf)
Original Date: 2018-08-25T19:45:02Z


Thanks for getting back to me. Yes that seems to fix the problem of sched_getcpu:

#include <cpuid.h>

#define CPUID(INFO, LEAF, SUBLEAF) __cpuid_count(LEAF, SUBLEAF, INFO[0], INFO[1], INFO[2], INFO[3])
</code>
class VlProfileRec {
private:
     int sched_getcpu() {
         uint32_t CPUInfo[4];
         CPUID(CPUInfo, 1, 0);
         /* CPUInfo[1] is EBX, bits 24-31 are APIC ID */
         if ((CPUInfo[3] & (1 << 9)) == 0) {
             return -1;  /* no APIC on chip */
         } else {
             return (unsigned)CPUInfo[1] >> 24;
         }
    }
...
</code>

Furthermore I had problems with randomisation. In particular clang doesn't like the re-entrant version of rand. My (non-semantically identical) workaround (verilated.cc):

#ifdef VL_THREADED
     static VL_THREAD_LOCAL bool t_seeded = false;
     // static VL_THREAD_LOCAL drand48_data t_buffer;
     static VerilatedMutex s_mutex;
     if (VL_UNLIKELY(!t_seeded)) {
	t_seeded = true;
	long seedval;
	{
	    VerilatedLockGuard lock(s_mutex);
	    seedval = lrand48()<<16 ^ lrand48();
	    if (!seedval) seedval++;
	}
	srand48(seedval);
     }
     long v0; v0 = lrand48();
     long v1; v1 = lrand48();
     return (v0<<16) ^ v1;
#elif defined(_WIN32) && !defined(__CYGWIN__)
     // Windows doesn't have lrand48(), although Cygwin does.
     return (rand()<<16) ^ rand();
#else
     return (lrand48()<<16) ^ lrand48();
#endif
}
</code>

That compiles for me.

Still I am having some SEGFAULTS on my DPI calls which I probably need to debug further (I enabled --threads-dpi none) so I can't tell you more about the functional state at the moment :(.

@veripoolbot
Copy link
Contributor Author

@veripoolbot veripoolbot commented Aug 25, 2018


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2018-08-25T21:54:41Z


Great, thanks, please check the git change I committed for sched_getcpu is good.

I don't understand your proposed VL_RAND. lrand() without the _r is not reentrant, it won't work when multithreaded.

@veripoolbot
Copy link
Contributor Author

@veripoolbot veripoolbot commented Aug 26, 2018


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2018-08-26T11:07:05Z


In git, I updated the random code to use its own algorithm, please try it.

@veripoolbot
Copy link
Contributor Author

@veripoolbot veripoolbot commented Aug 26, 2018


Original Redmine Comment
Author Name: Florian Zaruba (@zarubaf)
Original Date: 2018-08-26T18:48:58Z


The random number problem is solved. You forgot to add the macro and the include which is needed on OS X:

#include <cpuid.h>
#define CPUID(INFO, LEAF, SUBLEAF) __cpuid_count(LEAF, SUBLEAF, INFO[0], INFO[1], INFO[2], INFO[3])
</code>

Thanks

@veripoolbot
Copy link
Contributor Author

@veripoolbot veripoolbot commented Aug 26, 2018


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2018-08-26T23:55:21Z


Try, try again please.

@veripoolbot
Copy link
Contributor Author

@veripoolbot veripoolbot commented Aug 27, 2018


Original Redmine Comment
Author Name: Florian Zaruba (@zarubaf)
Original Date: 2018-08-27T01:55:40Z


Almost:

/usr/local/share/verilator/include/verilated_threads.h:200:64: error: expected ';' after asm statement
         __cpuid_count(1, 0, info[0], info[1], info[2], info[3])
</code>
@veripoolbot
Copy link
Contributor Author

@veripoolbot veripoolbot commented Aug 27, 2018


Original Redmine Comment
Author Name: Wilson Snyder (@wsnyder)
Original Date: 2018-08-27T10:16:06Z


All set then, thanks for your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.