Windows10 + WSL + vscode编程环境配置

准备工作

  1. Visual Studio Code

    下载vscode安装

  2. Windows Subsystem Linux

    使用Windows商店或者LxRunOffline安装Ubuntu-18.04lts

安装插件

打开vsvode,选择extension,安装Remote Development,获得Remote WSL.

1.png

此时左下角显示WSL字样,点击选择Remote-WSL:New Window,稍等片刻即连接到WSL

2.png

终端已切换至Linux命令行

3.png

在WSL中安装C/C++插件:

4.png

打开终端安装编译器

1
2
sudo apt-get update
sudo apt-get intall build-essential gdb

测试算例

新建一个项目目录,项目根目录添加文件夹命名为.vscode,在.vscode下新建两个文件launch.jsontasks.json,文件内容如下

launch.json

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
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

注意:externalConsole的作用是调用外部终端显示结果,针对WSL需设为falese,否则在调试时会卡住并出现Wait for connection completion.的问题。在launch.json中可以添加 “logging”: { “engineLogging”: true, “trace”: true, “traceResponse”: true }显示详细信息。

tasks.json

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
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
}
}
]
}

这两个文件也可以在Debug时根据自动生成的模板按实际情况修改。

添加测试代码

1
2
3
4
5
6
7
8
#include <cmath>
#include <fstream>
#include <iostream>

int main()
{
std::cout<<"hello world"<<std::endl;
}

添加断点,按F5进行调试

5.png

调用MKL函数库

下载

https://software.intel.com/en-us/mkl

安装

  1. 解压至任意目录(安装后可删除)

  2. 执行 sudo sh ./install.sh 默认安装至 /opt/, 可配置安装路径。

  3. 在 /etc/ld.so.conf.d 下创建名为 intel-mkl.conf 的文件,内容为

    1
    2
    /opt/intel/mkl/lib/intel64
    /opt/intel/lib/intel64
  4. 执行

    1
    2
    sudo ldconfig -v
    /opt/intel/mkl/bin/mklvars.sh intel64 mod

具体可参考官方说明

使用

测试代码下载地址,以dgemm_example.c为例

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#define min(x,y) (((x) < (y)) ? (x) : (y))
#include <stdio.h>
#include <stdlib.h>
#include "mkl.h"

int main()
{
double *A, *B, *C;
int m, n, p, i, j;
double alpha, beta;

printf ("\n This example computes real matrix C=alpha*A*B+beta*C using \n"
" Intel(R) MKL function dgemm, where A, B, and C are matrices and \n"
" alpha and beta are double precision scalars\n\n");

m = 2000, p = 200, n = 1000;
printf (" Initializing data for matrix multiplication C=A*B for matrix \n"
" A(%ix%i) and matrix B(%ix%i)\n\n", m, p, p, n);
alpha = 1.0; beta = 0.0;
printf (" Allocating memory for matrices aligned on 64-byte boundary for better \n"
" performance \n\n");
A = (double *)mkl_malloc( m*p*sizeof( double ), 64 );
B = (double *)mkl_malloc( p*n*sizeof( double ), 64 );
C = (double *)mkl_malloc( m*n*sizeof( double ), 64 );
if (A == NULL || B == NULL || C == NULL) {
printf( "\n ERROR: Can't allocate memory for matrices. Aborting... \n\n");
mkl_free(A);
mkl_free(B);
mkl_free(C);
return 1;
}

printf (" Intializing matrix data \n\n");
for (i = 0; i < (m*p); i++) {
A[i] = (double)(i+1);
}

for (i = 0; i < (p*n); i++) {
B[i] = (double)(-i-1);
}

for (i = 0; i < (m*n); i++) {
C[i] = 0.0;
}

printf (" Computing matrix product using Intel(R) MKL dgemm function via CBLAS interface \n\n");
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
m, n, p, alpha, A, p, B, n, beta, C, n);
printf ("\n Computations completed.\n\n");

printf (" Top left corner of matrix A: \n");
for (i=0; i<min(m,6); i++) {
for (j=0; j<min(p,6); j++) {
printf ("%12.0f", A[j+i*p]);
}
printf ("\n");
}

printf ("\n Top left corner of matrix B: \n");
for (i=0; i<min(p,6); i++) {
for (j=0; j<min(n,6); j++) {
printf ("%12.0f", B[j+i*n]);
}
printf ("\n");
}

printf ("\n Top left corner of matrix C: \n");
for (i=0; i<min(m,6); i++) {
for (j=0; j<min(n,6); j++) {
printf ("%12.5G", C[j+i*n]);
}
printf ("\n");
}

printf ("\n Deallocating memory \n\n");
mkl_free(A);
mkl_free(B);
mkl_free(C);

printf (" Example completed. \n\n");
return 0;
}

编译链接命令可参考Intel官方工具Intel® Math Kernel Library Link Line Advisor | Intel® Software

在环境变量中设置MKLROOT变量为MKL安装目录,并source生效

1
export MKLROOT="/opt/intel/mkl"

编译命令

g++ -c dgemm_example.c -fdefault-integer-8 -m64 -I${MKLROOT}/include

链接命令

g++ -o mkl_first.out dgemm_example.o -L${MKLROOT}/lib/intel64 -Wl,—no-as-needed -lmkl_gf_ilp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl

测试

项目结构

.
├── .vscode
│ ├── launch.json
│ └── tasks.json
├── Makefile
├── release
│ ├── dgemm_example
│ └── dgemm_example.o
└── src
​ └── dgemm_example.f

可由tree生成结构树,在wsl中输入

1
sudo apt-get install tree
  • tree -a 显示包括隐藏文件的所有文件
  • tree -d 只显示文件夹;
  • tree -L n 显示项目的层级。n表示层级数。比如想要显示项目三层结构,可以用tree -l 3;
  • tree -I pattern 用于过滤不想要显示的文件或者文件夹。比如你想要过滤项目中的node_modules文件夹,可以使用tree -I “node_modules”;
  • tree > tree.md 将项目结构输出到tree.md这个文件。

makefile

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
#==============================================================
#
# SAMPLE SOURCE CODE - SUBJECT TO THE TERMS OF SAMPLE CODE LICENSE AGREEMENT,
# http://software.intel.com/en-us/articles/intel-sample-source-code-license-agreement/
#
# Copyright 2005-2018 Intel Corporation
#
# THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE, NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS.
#
# =============================================================

FC := gfortran
SRCDIR := src
BUILDDIR := release
LIBFLAGS_LINK := -L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_gf_ilp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl
LIBFLAGS_COMPILER := -fdefault-integer-8 -m64 -I${MKLROOT}/include

all: $(BUILDDIR)/dgemm_example

$(BUILDDIR)/%: $(BUILDDIR)/%.o
$(FC) -g $< $(LIBFLAGS_LINK) -o $@

$(BUILDDIR)/%.o: $(SRCDIR)/%.f
@mkdir -p $(BUILDDIR)
$(FC) -g -c $< $(LIBFLAGS_COMPILER) -o $@

run_dgemm_example: $(BUILDDIR)/dgemm_example
./$(BUILDDIR)/dgemm_example

clean:
@echo " Cleaning..."
@rm -fr $(BUILDDIR) 2>/dev/null || true

.PHONY: clean
.PRECIOUS: $(BUILDDIR)/%.o

dgemm_example.f

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
!==============================================================
!
! SAMPLE SOURCE CODE - SUBJECT TO THE TERMS OF SAMPLE CODE LICENSE AGREEMENT,
! http://software.intel.com/en-us/articles/intel-sample-source-code-license-agreement/
!
! Copyright 2016-2018 Intel Corporation
!
! THIS FILE IS PROVIDED "AS IS" WITH NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
! NOT LIMITED TO ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
! PURPOSE, NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS.
!
! =============================================================
!*******************************************************************************
! This example computes real matrix C=alpha*A*B+beta*C using Intel(R) MKL
! subroutine DGEMM, where A,B, and C are matrices and alpha and beta are
! double precision scalars.
!
! In this simple example, practices such as memory management, data alignment,
! and I/O that are necessary for good programming style and high Intel(R) MKL
! performance are omitted to improve readability.
!*******************************************************************************

PROGRAM MAIN

IMPLICIT NONE

DOUBLE PRECISION ALPHA, BETA
INTEGER M, P, N, I, J
PARAMETER (M=2000, P=200, N=1000)
DOUBLE PRECISION A(M,P), B(P,N), C(M,N)

PRINT *, "This example computes real matrix C=alpha*A*B+beta*C"
PRINT *, "using Intel(R) MKL function dgemm, where A, B, and C"
PRINT *, "are matrices and alpha and beta are double precision "
PRINT *, "scalars"
PRINT *, ""

PRINT *, "Initializing data for matrix multiplication C=A*B for "
PRINT 10, " matrix A(",M," x",P, ") and matrix B(", P," x", N, ")"
10 FORMAT(a,I5,a,I5,a,I5,a,I5,a)
PRINT *, ""
ALPHA = 1.0
BETA = 0.0

PRINT *, "Intializing matrix data"
PRINT *, ""
DO I = 1, M
DO J = 1, P
A(I,J) = (I-1) * P + J
END DO
END DO

DO I = 1, P
DO J = 1, N
B(I,J) = -((I-1) * N + J)
END DO
END DO

DO I = 1, M
DO J = 1, N
C(I,J) = 0.0
END DO
END DO

PRINT *, "Computing matrix product using Intel(R) MKL DGEMM "
PRINT *, "subroutine"
CALL DGEMM('N','N',M,N,P,ALPHA,A,M,B,P,BETA,C,M)
PRINT *, "Computations completed."
PRINT *, ""

PRINT *, "Top left corner of matrix A:"
PRINT 20, ((A(I,J), J = 1,MIN(P,6)), I = 1,MIN(M,6))
PRINT *, ""

PRINT *, "Top left corner of matrix B:"
PRINT 20, ((B(I,J),J = 1,MIN(N,6)), I = 1,MIN(P,6))
PRINT *, ""

20 FORMAT(6(F12.0,x))

PRINT *, "Top left corner of matrix C:"
PRINT 30, ((C(I,J), J = 1,MIN(N,6)), I = 1,MIN(M,6))
PRINT *, ""

30 FORMAT(6(ES12.4,x))

PRINT *, "Example completed."
STOP

END

launch.json

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
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/release/dgemm_example",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "shell",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

tasks.json

1
2
3
4
5
6
7
8
9
10
{
"version": "2.0.0",
"tasks": [
{
"label": "shell", // 任务名称,与launch.json的preLaunchTask相对应
"command": "make", // 在shell中使用命令,如需加参数,可再添加args属性
"type":"shell"
}
]
}

结果

打开makefile,F5调试

6.png

Python环境搭建

  1. 首先在WSL下安装Anaconda

  2. 在vscode扩展插件商店里搜索python并安装至WSL

    1.JPG

  3. Ctrl+Shift+P打开搜索栏,输入Remote Settings

    2.JPG

  4. 配置pylint和python路径pylintPath和pythonPath

    3.JPG