How can I compile a program to link MKL dynamically and so that it only uses the basic DLLs (mkl_intel_thread.dll, mkl_core.dll, libiomp5md.dll) and won't try to load different ones like mkl_avx.dll, mkl_avx2.dll, etc. on different processors?
Or is there an environment variable or something to do that?
you don't need to link explicitly about this dll - it happens automatically. You also may try to build your owner custom dll which will contain all optimizations for all cpu types. You may refer to the MKL User Guide - see Building Custom Dynamic-link Libraries.
Oh, but surely that's what will be used on an older CPU, isn't it? I find that results with mkl_avx2.dll appear to be wrong in one case but are much better with mkl_def.dll.
Another problem is the large size of all those files which makes distribution a bit less convenient.
if the results are wrong, make sense to give us the example that shows this problem. You may also try to submit the ticket against MKL into the intel online service center (https://supporttickets.intel.com/).
Unfortunately, it's not my software so it would be quite a lot of work to make an example the clearly shows the problem. I'm not even sure it's really a bug. The results with mkl_avx2.dll have about 10x the magnitude of random noise in the solution as it does with only mkl_def.dll. Both have this noise though so it might be an ill-conditioned problem I'm trying to solve or in some other way pushing it beyond what it's meant to do.
I'm confused about why using only mkl_def.dll is risky though. Is that not what it falls back on when the other features like AVX and SIMD aren't available? Surely it's safe on an older processor without those extra features?