summaryrefslogtreecommitdiff
path: root/libgomp/testsuite/libgomp.c/alloc-pinned-7.c
blob: 8dc190550380a216bf0ae9cf7025faca428be06a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* { dg-do run } */
/* { dg-additional-options "-foffload-memory=pinned" } */

/* { dg-xfail-run-if "Pinning not implemented on this host" { ! *-*-linux-gnu } } */

/* Test that pinned memory works.  */

#include <stdio.h>
#include <stdlib.h>

#ifdef __linux__
#include <sys/types.h>
#include <unistd.h>

#include <sys/mman.h>

int
get_pinned_mem ()
{
  int pid = getpid ();
  char buf[100];
  sprintf (buf, "/proc/%d/status", pid);

  FILE *proc = fopen (buf, "r");
  if (!proc)
    abort ();
  while (fgets (buf, 100, proc))
    {
      int val;
      if (sscanf (buf, "VmLck: %d", &val))
	{
	  fclose (proc);
	  return val;
	}
    }
  abort ();
}
#else
int
get_pinned_mem ()
{
  return 0;
}

#define mlockall(...) 0
#endif

#include <omp.h>

int
main ()
{
  // Sanity check
  if (get_pinned_mem () == 0)
    {
      /* -foffload-memory=pinned has failed, but maybe that's because
	 isufficient pinned memory was available.  */
      if (mlockall (MCL_CURRENT | MCL_FUTURE) == 0)
	abort ();
    }

  return 0;
}