From f467a8b3a3e986333c8f4382b5c9acae2f34c4e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20H=C3=B6nig?= <jan.hoenig@fau.de>
Date: Thu, 11 Nov 2021 18:31:46 +0100
Subject: [PATCH] Introducing the CI pipeline

---
 .gitignore                |   1 +
 .gitlab-ci.yml            |  32 +++++++++++
 Dockerfile                |  18 ++++++
 test.py => examle/test.py |   0
 requirements.txt          |   3 +-
 tests/test_benchmark.py   |  26 +++++++++
 todo_gitlab-ci.yml        | 113 --------------------------------------
 tox.ini                   |   2 +-
 8 files changed, 80 insertions(+), 115 deletions(-)
 create mode 100644 .gitlab-ci.yml
 create mode 100644 Dockerfile
 rename test.py => examle/test.py (100%)
 create mode 100755 tests/test_benchmark.py
 delete mode 100644 todo_gitlab-ci.yml

diff --git a/.gitignore b/.gitignore
index 5410ab3..5ec93d6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 .idea/
+.tox/
 build/
 venv/
 pystencils_benchmark.egg-info/
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..8a81d77
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,32 @@
+stages:
+  - docker-build
+  - test
+
+# ---------------------- Docker-build ---------------------
+build-docker:
+  stage: docker-build
+  image: docker:latest
+  script:
+    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
+    - docker build --pull . -f Dockerfile -t i10git.cs.fau.de:5005/hoenig/pystencils-benchmark/pystencils-benchmark
+    - docker push i10git.cs.fau.de:5005/hoenig/pystencils-benchmark/pystencils-benchmark
+  tags:
+    - docker-docker
+  rules:
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      changes:
+        - Dockerfile
+    - when: manual
+      allow_failure: true
+
+
+# ---------------------- Tests ---------------------
+test:
+  stage: test
+  image: i10git.cs.fau.de:5005/hoenig/pystencils-benchmark/pystencils-benchmark
+  tags:
+    - docker
+  script:
+    - pip install tox
+    - echo $TOX_ENV
+    - tox
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..1e1847e
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,18 @@
+FROM ubuntu:latest
+
+LABEL maintainer="jan.hoenig@fau.de"
+LABEL version="0.1"
+LABEL description="Custom docker image for pystencils-benchmark"
+
+ARG DEBIAN_FRONTEND=noninteractive
+
+RUN apt-get update
+
+RUN apt-get install -y \
+    python3 \
+    build-essential \
+    clang \
+    gcc && \
+    rm -rf /var/lib/apt/lists/* && \
+    apt-get clean
+
diff --git a/test.py b/examle/test.py
similarity index 100%
rename from test.py
rename to examle/test.py
diff --git a/requirements.txt b/requirements.txt
index 14f8297..16a3353 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,5 @@
 Jinja2>=3.0.2
 pystencils>=0.4.1
 setuptools>=44.0.0
-numpy>=1.21.4
\ No newline at end of file
+numpy>=1.21.4
+sympy>=1.9
\ No newline at end of file
diff --git a/tests/test_benchmark.py b/tests/test_benchmark.py
new file mode 100755
index 0000000..b84aef3
--- /dev/null
+++ b/tests/test_benchmark.py
@@ -0,0 +1,26 @@
+import subprocess
+import numpy as np
+import sympy as sp
+import pystencils as ps
+from pystencils_benchmark import generate_benchmark, Compiler
+from pathlib import Path
+
+
+def test_generate(path: Path):
+    a, b, c = ps.fields(a=np.ones(4000000), b=np.ones(4000000), c=np.ones(4000000))
+    alpha = sp.symbols('alpha')
+
+    @ps.kernel_config(ps.CreateKernelConfig())
+    def vadd():
+        a[0] @= b[0] + c[0]
+    kernel_vadd = ps.create_kernel(**vadd)
+
+    @ps.kernel_config(ps.CreateKernelConfig())
+    def daxpy():
+        b[0] @= alpha * a[0] + b[0]
+    kernel_daxpy = ps.create_kernel(**daxpy)
+
+    for compiler in [Compiler.GCC, Compiler.GCCdebug, Compiler.Clang]:
+        generate_benchmark([kernel_vadd, kernel_daxpy], path, compiler=compiler)
+        subprocess.run(['make'])
+        subprocess.run([f'./benchmark-{compiler.name}', '100'])
diff --git a/todo_gitlab-ci.yml b/todo_gitlab-ci.yml
deleted file mode 100644
index a30b10c..0000000
--- a/todo_gitlab-ci.yml
+++ /dev/null
@@ -1,113 +0,0 @@
-stages:
-  - test
-  - build
-  - example-build
-  - example-test
-
-
-# ---------------------- Tests ---------------------
-.test_template: &test_definition
-  stage: test
-  tags:
-    - docker
-  script:
-    - pip install tox
-    - echo $TOX_ENV
-    - tox -e $TOX_ENV
-  artifacts:
-    when: always
-    reports:
-      junit:
-        - report.xml
-        
-test-3.10:
-  image: python:3.10
-  variables:
-    TOX_ENV: py310
-  <<: *test_definition
-
-test-3.9:
-  image: python:3.9
-  variables:
-    TOX_ENV: py39
-  <<: *test_definition
-
-test-3.8:
-  image: python:3.8
-  variables:
-    TOX_ENV: py38
-  <<: *test_definition
-
-test-docs:
-  image: python:3.10
-  tags:
-    - docker
-  script:
-    - pip install tox
-    - tox -e docs
-
-
-# ------------------ Create documentation, image, pypy from studon-scripts -----------------
-pages:
-   stage: build
-   image: python:3.10
-   script:
-     - pip install tox
-     - tox -e docs
-     - mv html_doc public
-
-   artifacts:
-     paths:
-       - public
-   tags:
-     - docker
-   only:
-     - master
-
-build-docker:
-   stage: build
-   image: docker:latest
-   script:
-     - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-     - docker build --pull . -f Dockerfile -t i10git.cs.fau.de:5005/teaching/studon-scripts/studon-scripts
-     - docker push i10git.cs.fau.de:5005/teaching/studon-scripts/studon-scripts
-   tags:
-      - docker-docker
-   when: manual
-
-build-pypi:
-  stage: build
-  image: python:3.10
-  script:
-    - pip install twine
-    - python3 setup.py sdist bdist_wheel
-    - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python -m twine upload --verbose --repository-url https://i10git.cs.fau.de/api/v4/projects/${CI_PROJECT_ID}/packages/pypi dist/*
-  tags:
-    - docker
-  only:
-    refs:
-      - master
-    changes:
-      - setup.cfg
-
-# ------------------- Create an image for the example problem ---------------
-example-build:
-  stage: example-build
-  image: docker:latest
-  script:
-    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-    - docker build --build-arg READ_API=$READ_API --pull . -f ./test_resources/example/Dockerfile -t i10git.cs.fau.de:5005/teaching/studon-scripts/example
-    - docker push i10git.cs.fau.de:5005/teaching/studon-scripts/example
-  tags:
-    - docker-docker
-  when: manual
-
-# ------------------ Test created image on an example problem ---------------
-example-test:
-  stage: example-test
-  image: i10git.cs.fau.de:5005/teaching/studon-scripts/example
-  script:
-    - cd test_resources/example
-    - bash example.sh
-  tags:
-    - docker
diff --git a/tox.ini b/tox.ini
index 294342c..28c7cf2 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,5 +1,5 @@
 [tox]
-envlist = py38, py39, py310
+envlist = py39
 requires = setuptools >= 40.0.0
 
 [testenv]
-- 
GitLab