Skip to content

Instantly share code, notes, and snippets.

@nattoheaven
Created September 4, 2013 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nattoheaven/6438054 to your computer and use it in GitHub Desktop.
Save nattoheaven/6438054 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <immintrin.h>
static volatile double zr = 0.0;
static volatile double zi = 0.0;
static const double cr = 0.1;
static const double ci = 0.1;
static void
*run(void *arg)
{
for (int i = 0; i < 1000000; ++i) {
while (true) {
if (_xbegin() == _XBEGIN_STARTED) {
double oldzr = zr;
double oldzi = zi;
double newzr = oldzr * oldzr - oldzi * oldzi + cr;
double newzi = 2 * oldzr * oldzi + ci;
zr = newzr;
zi = newzi;
_xend();
break;
}
_mm256_zeroall();
}
}
return 0;
}
int
main(int argc, char **argv)
{
int n = atoi(argv[1]);
pthread_t *threads = new pthread_t[n];
for (int i = 0; i < n; ++i) {
pthread_create(&threads[i], 0, run, 0);
}
for (int i = 0; i < n; ++i) {
pthread_join(threads[i], 0);
}
printf("%f + %fi\n", zr, zi);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment