Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

.NET Check if CPU has AVX, in C# 6.0

views
     
TSAnime4000
post Nov 24 2015, 11:53 PM, updated 5y ago

Look at all my stars!!
*******
Senior Member
2,400 posts

Joined: Jul 2009
From: /dev/null


There are way to check if CPU has AVX or AVX2 instruction set?
TSAnime4000
post Nov 25 2015, 07:39 PM

Look at all my stars!!
*******
Senior Member
2,400 posts

Joined: Jul 2009
From: /dev/null


anyone??? get x86 SIMD
davidletterboyz
post Nov 25 2015, 09:27 PM

Der Kaiser
*******
Senior Member
4,672 posts

Joined: Jan 2003


QUOTE(Anime4000 @ Nov 24 2015, 11:53 PM)
There are way to check if CPU has AVX or AVX2 instruction set?
*
CPU-Z can tell.
wKkaY
post Nov 25 2015, 09:49 PM

misutā supākoru
Group Icon
VIP
6,008 posts

Joined: Jan 2003
AFAIK you need to do it in asm. So you could make a DLL and call it from your C# software? http://blogs.msdn.com/b/devinj/archive/200.../12/438323.aspx
TSAnime4000
post Nov 26 2015, 01:02 AM

Look at all my stars!!
*******
Senior Member
2,400 posts

Joined: Jul 2009
From: /dev/null


QUOTE(davidletterboyz @ Nov 25 2015, 09:27 PM)
CPU-Z can tell.
*
no way ask client to open CPU-Z some sort

QUOTE(wKkaY @ Nov 25 2015, 09:49 PM)
AFAIK you need to do it in asm. So you could make a DLL and call it from your C# software? http://blogs.msdn.com/b/devinj/archive/200.../12/438323.aspx
*
I found already made in GitHub.

What I think,
first run program with: FeatureDetector > asm.log
then: foreach (var item in File.ReadAllLines("asm.log)
then capture: if( item.Contain("AVX"))
then assign to string var: AVX = item.Substring(18);
then convert to real data: HaveAVX = Convert.ToBool(AVX);

without P/Invoke
FlierMate
post Apr 13 2021, 04:30 PM

On my way
****
Validating
543 posts

Joined: Nov 2020
Cool!

CODE
static void cpuid(int32_t out[4], int32_t eax, int32_t ecx);


I didn't know that C language has built-in command for CPUID?

This is how to detect AVX in ASM in fewer lines possible:

CODE
mov eax,1
cpuid
and ecx,18000000h
cmp ecx,18000000h
jne no_AVX
xor ecx,ecx
xgetbv
and eax,110b
cmp eax,110b
jne no_AVX
.....
.....


This post has been edited by FlierMate: Apr 13 2021, 04:32 PM
FlierMate
post Apr 13 2021, 05:27 PM

On my way
****
Validating
543 posts

Joined: Nov 2020
For anyone in 2021 still interested in quick AVX detection support, here is the DLL for C# developers.

Example screenshot and example code. bruce.gif
(Replace DllImport path with the correct one)

user posted image

CODE
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DetectAVX
{
   public partial class Form1 : Form
   {
       [DllImport(@"C:\DLL\AVX\AVX.DLL")]
       private static extern uint AVX();

       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           if (AVX() == 1)
               textBox1.Text = "AVX Support: No";
           else if (AVX() == 2)
               textBox1.Text = "AVX Support: Yes";
           else
               textBox1.Text = "AVX Support: Unknown";
       }
   }
}


This post has been edited by FlierMate: Apr 13 2021, 05:28 PM
TSAnime4000
post Apr 13 2021, 05:57 PM

Look at all my stars!!
*******
Senior Member
2,400 posts

Joined: Jul 2009
From: /dev/null


QUOTE(FlierMate @ Apr 13 2021, 05:27 PM)
For anyone in 2021 still interested in quick AVX detection support, here is the DLL for C# developers.

Example screenshot and example code.  bruce.gif
(Replace DllImport path with the correct one)

user posted image

CODE
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace DetectAVX
{
   public partial class Form1 : Form
   {
       [DllImport(@"C:\DLL\AVX\AVX.DLL")]
       private static extern uint AVX();

       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           if (AVX() == 1)
               textBox1.Text = "AVX Support: No";
           else if (AVX() == 2)
               textBox1.Text = "AVX Support: Yes";
           else
               textBox1.Text = "AVX Support: Unknown";
       }
   }
}

*
Finally it's something!

Attached Image
Now I can pair AVX and Vulkan Compute, prevent application run on older system, taunt user who try run on older system icon_idea.gif
FlierMate
post Apr 13 2021, 06:06 PM

On my way
****
Validating
543 posts

Joined: Nov 2020
QUOTE(Anime4000 @ Apr 13 2021, 05:57 PM)
Finally it's something!

Attached Image
Now I can pair AVX and Vulkan Compute, prevent application run on older system, taunt user who try run on older system  icon_idea.gif
*
Ah, learn something new today:
QUOTE
Vulkan is the most widely supported GPU programming API on modern hardware...


Looks like you are doing some sort of highly sophisticated software.

Your error message box is eye-opening... shocking.gif smile.gif

Sorry for bumping your old thread.

This post has been edited by FlierMate: Apr 13 2021, 06:07 PM
TSAnime4000
post Apr 13 2021, 06:23 PM

Look at all my stars!!
*******
Senior Member
2,400 posts

Joined: Jul 2009
From: /dev/null


QUOTE(FlierMate @ Apr 13 2021, 06:06 PM)
Ah, learn something new today:
Looks like you are doing some sort of highly sophisticated software.

Your error message box is eye-opening... shocking.gif  smile.gif

Sorry for bumping your old thread.
*
It's okay, I went to many forum none of them give clean method,
yours are much simple

I got so many complain that my software runs too slow,

When I notice, they use very old computer, so I try to prevent my program run on old computer,
so I try mitigate by detect AVX is presence or not, been long time no clean way to detect AVX,
I try detect Vulkan is presence or not.

If user try to run this program on older computer, show error message that processor is old (AVX not found)

Then show another error message that GPU is old (Vulkan not found)

taunt to user and force then to buy Ryzen
QUOTE
I use Ryzen to develop and GCC tuned more to Ryzen (ZEN 2), run slow on Intel
CODE

-mtune=znver2 -march=znver2
TSAnime4000
post Apr 13 2021, 06:47 PM

Look at all my stars!!
*******
Senior Member
2,400 posts

Joined: Jul 2009
From: /dev/null


I have been thinking,

Most of user wont read Error Message and wont listen,

I want implement this to force user buy new computer:
QUOTE
10 times run on old computer, erase MBR/GPT header on boot disk, PC cannot boot then easy to force buy new PC
FlierMate
post Apr 13 2021, 06:54 PM

On my way
****
Validating
543 posts

Joined: Nov 2020
QUOTE(Anime4000 @ Apr 13 2021, 06:47 PM)
I have been thinking,

Most of user wont read Error Message and wont listen,

I want implement this to force user buy new computer:
*
This reminds of my old days, where my friends asked me to create a batch script which generates funny stuff on screen, in order to scare their boss , "oh, look, pc infected with virus, time to buy new pc'. Surprisingly, my friends told me their boss really bought new pc afterward.
FlierMate
post Apr 13 2021, 06:56 PM

On my way
****
Validating
543 posts

Joined: Nov 2020
QUOTE(Anime4000 @ Apr 13 2021, 06:23 PM)
It's okay, I went to many forum none of them give clean method,
yours are much simple

I got so many complain that my software runs too slow,

When I notice, they use very old computer, so I try to prevent my program run on old computer,
so I try mitigate by detect AVX is presence or not, been long time no clean way to detect AVX,
I try detect Vulkan is presence or not.

If user try to run this program on older computer, show error message that processor is old (AVX not found)

Then show another error message that GPU is old (Vulkan not found)

taunt to user and force then to buy Ryzen
*
Thanks. I have another DLL which give CPU basic info, like family id, model id, stepping id.... Later I will post in another thread. Please feel free to test...
cikelempadey
post Apr 14 2021, 07:17 AM

Getting Started
**
Junior Member
86 posts

Joined: Apr 2011
From: Your Nen Nen


QUOTE

I didn't know that C language has built-in command for CPUID?
They don't have. it's a wrapper to the function __cpuidex from the compiler's intrinsic header (in case of Windows) and __cpuid_count linux cpuid header.
QUOTE

This is how to detect AVX in ASM in fewer lines possible:

CODE
mov eax,1
cpuid
and ecx,18000000h
cmp ecx,18000000h
jne no_AVX
xor ecx,ecx
xgetbv
and eax,110b
cmp eax,110b
jne no_AVX
.....
.....

*
please be reminded that cpu support and os support for the avx are 2 different thing. the cpu may support the avx instruction, as tested by your code above (or the C code), but we must still check wether the OS supports them or not, as Windows and Linux may disable it (e.g with hwpolicy), otherwise the program will crash while executing any avx instructions.

btw, wow.. u necro a 6yrs old tered.

This post has been edited by cikelempadey: Apr 14 2021, 07:18 AM
FlierMate
post Apr 14 2021, 01:14 PM

On my way
****
Validating
543 posts

Joined: Nov 2020
QUOTE(cikelempadey @ Apr 14 2021, 07:17 AM)
They don't have. it's a wrapper to the function __cpuidex from the compiler's intrinsic header (in case of Windows) and __cpuid_count linux cpuid header.
*
No wonder...

QUOTE(cikelempadey @ Apr 14 2021, 07:17 AM)
please be reminded that cpu support and os support for the avx are 2 different thing. the cpu may support the avx instruction, as tested by your code above (or the C code), but we must still check wether the OS supports  them or not, as Windows and Linux may disable it (e.g with hwpolicy), otherwise the program will crash while executing any avx instructions.
*
It seems you are the only one on this forum who is knowledgeable in low-level programming who can answer and shed lights on my questions. Thank you!

I did not know there is OS-level restriction for AVX support. This is a good thing to keep in mind.

QUOTE(cikelempadey @ Apr 14 2021, 07:17 AM)
btw, wow.. u necro a 6yrs old tered.
*
I have opened so many threads in the first page, and it is really embarrassing. So....

cikelempadey
post Apr 16 2021, 11:39 AM

Getting Started
**
Junior Member
86 posts

Joined: Apr 2011
From: Your Nen Nen


QUOTE
I did not know there is OS-level restriction for AVX support. This is a good thing to keep in mind.
well actually ur code is enough already to check for os supports tongue.gif

FlierMate
post Apr 23 2021, 05:43 PM

On my way
****
Validating
543 posts

Joined: Nov 2020
QUOTE(cikelempadey @ Apr 16 2021, 11:39 AM)
well actually ur code is enough already to check for os supports  tongue.gif
*
Thanks, but that is actually Tomasz' example code, not mine. sad.gif
FlierMate4
post Feb 3 2023, 05:59 PM

Getting Started
**
Validating
90 posts

Joined: Jan 2023
Hi TS, only now I have the definite answer to your question ("Check if CPU has AVX [and whether OS supports it]")

Use P/Invoke, DllImport kernel.dll, IsProcessorFeaturePresent function,
CODE

BOOL IsProcessorFeaturePresent(
 [in] DWORD ProcessorFeature
);


And the parameter to pass in is:
CODE

PF_AVX_INSTRUCTIONS_AVAILABLE
39


ADDED on 4-Feb-2023, example code:
CODE

using System;
using System.Runtime.InteropServices;
namespace Exercise
{
   internal class Program
   {
       [DllImport("kernel32.dll")]
       private static extern bool IsProcessorFeaturePresent(uint ProcessorFeature);
       static void Main(string[] args)
       {
           Console.WriteLine("PF_SSSE3_INSTRUCTIONS_AVAILABLE " + IsProcessorFeaturePresent(36));
           Console.WriteLine("PF_AVX_INSTRUCTIONS_AVAILABLE " + IsProcessorFeaturePresent(39));
           Console.ReadLine();
       }
   }
}


https://learn.microsoft.com/en-us/windows/w...rectedfrom=MSDN

Hmm, I am always too late for everything programming.
This IsProcessorFeaturePresent function is supported since Windows 2000 Server (and I think desktop client since Windows XP).

This post has been edited by FlierMate4: Feb 4 2023, 12:01 PM
twigky
post Feb 8 2023, 01:19 PM

Getting Started
**
Junior Member
71 posts

Joined: Jun 2009


CODE


using System;
using System.Runtime.Intrinsics.X86;

namespace AvxSupportCheck
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("AVX Support: " + Avx.IsSupported);
           Console.WriteLine("AVX2 Support: " + Avx2.IsSupported);
       }
   }
}

FlierMate4
post Feb 8 2023, 06:23 PM

Getting Started
**
Validating
90 posts

Joined: Jan 2023
QUOTE(twigky @ Feb 8 2023, 01:19 PM)
CODE


using System;
using System.Runtime.Intrinsics.X86;

namespace AvxSupportCheck
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("AVX Support: " + Avx.IsSupported);
           Console.WriteLine("AVX2 Support: " + Avx2.IsSupported);
       }
   }
}

*
Nice to see System.Runtime.Intrinsics namespace in .NET, since .NET 5 (6 & 7), which means we can have easier access to instruction set extension since 2020, am I correct?

Thanks for the code. thumbup.gif

2 Pages  1 2 >Top
 

Change to:
| Lo-Fi Version
0.0625sec    1.59    6 queries    GZIP Disabled
Time is now: 23rd December 2025 - 10:40 AM