From 97385e2349ecb7081b33527661ddc9cd900caf4c Mon Sep 17 00:00:00 2001 From: Qi Zheng Date: Fri, 10 Jan 2020 08:46:54 +0000 Subject: [PATCH] Add node and numpy-mp openfaas templates Signed-off-by: Qi Zheng --- FaaS/OpenFaaS/template/README.md | 84 ++----------- .../template/dockerfile-clearlinux/README.md | 23 ++++ .../template/go-clearlinux/Dockerfile | 4 +- .../OpenFaaS/template/go-clearlinux/README.md | 22 ++++ .../go-clearlinux/{ => function}/bundles.txt | 0 .../template/node-clearlinux/Dockerfile | 62 ++++++++++ .../template/node-clearlinux/README.md | 28 +++++ .../function}/bundles.txt | 0 .../node-clearlinux/function/handler.js | 12 ++ .../node-clearlinux/function/package.json | 12 ++ .../template/node-clearlinux/index.js | 113 ++++++++++++++++++ .../template/node-clearlinux/package.json | 16 +++ .../template/node-clearlinux/template.yml | 7 ++ FaaS/OpenFaaS/template/numpy-mp/Dockerfile | 39 ++++++ FaaS/OpenFaaS/template/numpy-mp/README.md | 40 +++++++ .../function/bundles.txt} | 0 .../template/numpy-mp/function/handler.py | 7 ++ .../numpy-mp/function/helper_script.sh | 1 + .../template/numpy-mp/function/main.py | 14 +++ .../template/numpy-mp/function/numpy-entry.sh | 9 ++ .../numpy-mp/function/requirements.txt | 0 .../numpy-mp/function/set-num-threads.sh | 57 +++++++++ FaaS/OpenFaaS/template/numpy-mp/template.yml | 2 + .../template/python3-clearlinux/Dockerfile | 10 -- .../template/python3-clearlinux/README.md | 34 ++++++ 25 files changed, 512 insertions(+), 84 deletions(-) create mode 100644 FaaS/OpenFaaS/template/dockerfile-clearlinux/README.md create mode 100644 FaaS/OpenFaaS/template/go-clearlinux/README.md rename FaaS/OpenFaaS/template/go-clearlinux/{ => function}/bundles.txt (100%) create mode 100644 FaaS/OpenFaaS/template/node-clearlinux/Dockerfile create mode 100644 FaaS/OpenFaaS/template/node-clearlinux/README.md rename FaaS/OpenFaaS/template/{python3-clearlinux => node-clearlinux/function}/bundles.txt (100%) create mode 100644 FaaS/OpenFaaS/template/node-clearlinux/function/handler.js create mode 100644 FaaS/OpenFaaS/template/node-clearlinux/function/package.json create mode 100644 FaaS/OpenFaaS/template/node-clearlinux/index.js create mode 100644 FaaS/OpenFaaS/template/node-clearlinux/package.json create mode 100644 FaaS/OpenFaaS/template/node-clearlinux/template.yml create mode 100644 FaaS/OpenFaaS/template/numpy-mp/Dockerfile create mode 100644 FaaS/OpenFaaS/template/numpy-mp/README.md rename FaaS/OpenFaaS/template/{python3-clearlinux/requirements.txt => numpy-mp/function/bundles.txt} (100%) create mode 100644 FaaS/OpenFaaS/template/numpy-mp/function/handler.py create mode 100644 FaaS/OpenFaaS/template/numpy-mp/function/helper_script.sh create mode 100644 FaaS/OpenFaaS/template/numpy-mp/function/main.py create mode 100644 FaaS/OpenFaaS/template/numpy-mp/function/numpy-entry.sh create mode 100644 FaaS/OpenFaaS/template/numpy-mp/function/requirements.txt create mode 100644 FaaS/OpenFaaS/template/numpy-mp/function/set-num-threads.sh create mode 100644 FaaS/OpenFaaS/template/numpy-mp/template.yml create mode 100644 FaaS/OpenFaaS/template/python3-clearlinux/README.md diff --git a/FaaS/OpenFaaS/template/README.md b/FaaS/OpenFaaS/template/README.md index 14685de..91628d6 100644 --- a/FaaS/OpenFaaS/template/README.md +++ b/FaaS/OpenFaaS/template/README.md @@ -39,86 +39,26 @@ Steps could refer to [OpenFaaS workshop](https://github.com/openfaas/workshop) -### dockerfile-clearlinux -1. `mkdir test && cd test` -2. `cp -r ""/FaaS/OpenFaaS/template/. template/` -3. `faas-cli new --lang dockerfile-clearlinux hello-dockerfile --prefix=""` +### Get Clear Linux based templates - Files tree as below. -> - ├── hello-dockerfile - │   ├── bundles.txt - │   └── Dockerfile - ├── hello-dockerfile.yml +```shell +$ mkdir test && cd test +$ git clone https://github.com/clearlinux/dockerfiles.git +$ cp -r dockerfiles/FaaS/OpenFaaS/template . +``` - * Put the required Clear Linux bundles in the "bundles.txt". - For example, +### For each template how-to, please refer to - `echo "openblas" >> hello-dockerfile/bundles.txt` - `echo "wget" >> hello-dockerfile/bundles.txt` -4. Replace the "cat" in the Dockerfile line below per your requirement. - ENV fprocess="cat" -5. `faas-cli up -f hello-dockerfile.yml` +* [dockerfile-clearlinux](./dockerfile-clearlinux/README.md) - Then you can invoke your python function by OpenFaas UI or faas-cli. +* [go-clearlinux](./go-clearlinux/README.md) -### python3-clearlinux -1. `mkdir test && cd test` -2. `cp -r ""/FaaS/OpenFaaS/template/. template/` -3. `faas-cli new --lang python3-clearlinux hello-openfaas --prefix=""` +* [node-clearlinux](./node-clearlinux/README.md) - Files tree as below. -> - ├── hello-openfaas - │   ├── bundles.txt - │   ├── handler.py - │   ├── helper_script.sh - │   ├── __init__.py - │   └── requirements.txt - ├── hello-openfaas.yml - └── template +* [numpy-mp](./numpy-mp/README.md) +* [python3-clearlinux](./python3-clearlinux/README.md) - * Put the required python packages in the "requirements.txt". - For example, - - `echo "redis" >> hello-openfaas/requirements.txt` - `echo "flask" >> hello-openfaas/requirements.txt` - - * Put the required Clear Linux bundles in the "bundles.txt". - For example, - - `echo "openblas" >> hello-openfaas/bundles.txt` - `echo "wget" >> hello-openfaas/bundles.txt` - - * Put any initial/helper operation in the "help_script.sh" - - -4. `faas-cli up -f hello-openfaas.yml` - - Then you can invoke your python function by OpenFaas UI or faas-cli. - -### go-clearlinux -1. mkdir test && cd test -2. `cp -r ""/FaaS/OpenFaaS/template/. template/` -3. `faas-cli new --lang go-clearlinux go-openfaas --prefix=""` - - Files tree as below. -> - ├── go-openfaas - │   ├── handler.go - ├── go-openfaas.yml - └── template - - - * Put the required Clear Linux bundles in the "bundles.txt". - For example, - - `echo "openblas" >> go-openfaas/bundles.txt` - `echo "wget" >> go-openfaas/bundles.txt` -4. `faas-cli up -f go-openfaas.yml` - - Then you can invoke your go function by OpenFaas UI or faas-cli. ## Proxy and Clear Linux mirror diff --git a/FaaS/OpenFaaS/template/dockerfile-clearlinux/README.md b/FaaS/OpenFaaS/template/dockerfile-clearlinux/README.md new file mode 100644 index 0000000..09c8148 --- /dev/null +++ b/FaaS/OpenFaaS/template/dockerfile-clearlinux/README.md @@ -0,0 +1,23 @@ +Based on clearlinux:base image + +### dockerfile-clearlinux +1. `faas-cli new --lang dockerfile-clearlinux hello-openfaas --prefix=""` + + Files tree as below. +> + ├── hello-openfaas + │   ├── bundles.txt + │   └── Dockerfile + ├── hello-openfaas.yml + └── template + + * Put the required Clear Linux bundles in the "bundles.txt". + For example, + + `echo "openblas" >> hello-openfaas/bundles.txt` + `echo "wget" >> hello-openfaas/bundles.txt` + +2. `faas-cli up -f hello-openfaas.yml` + + Then you can invoke your python function by OpenFaas UI or faas-cli. + diff --git a/FaaS/OpenFaaS/template/go-clearlinux/Dockerfile b/FaaS/OpenFaaS/template/go-clearlinux/Dockerfile index 90f40c0..81dc1f7 100644 --- a/FaaS/OpenFaaS/template/go-clearlinux/Dockerfile +++ b/FaaS/OpenFaaS/template/go-clearlinux/Dockerfile @@ -13,7 +13,7 @@ COPY . . # install bundles if required # PLEASE input bundle line by line -COPY bundles.txt . +COPY function/bundles.txt . RUN for bundle in $(cat bundles.txt); do \ swupd bundle-add $bundle $swupd_args; \ done @@ -35,7 +35,7 @@ WORKDIR /home/app # install bundles if required # PLEASE input bundle line by line -COPY bundles.txt . +COPY function/bundles.txt . RUN for bundle in $(cat bundles.txt); do \ swupd bundle-add $bundle $swupd_args; \ done diff --git a/FaaS/OpenFaaS/template/go-clearlinux/README.md b/FaaS/OpenFaaS/template/go-clearlinux/README.md new file mode 100644 index 0000000..2059735 --- /dev/null +++ b/FaaS/OpenFaaS/template/go-clearlinux/README.md @@ -0,0 +1,22 @@ +Based on clearlinux/golang container image + +### go-clearlinux +1. `faas-cli new --lang go-clearlinux go-openfaas --prefix=""` + + Files tree as below. +> + . + ├── go-openfaas + │   ├── bundles.txt + │   └── handler.go + ├── go-openfaas.yml + └── template + + * Put the required Clear Linux bundles in the "bundles.txt". + For example, + + `echo "openblas" >> go-openfaas/bundles.txt` + `echo "wget" >> go-openfaas/bundles.txt` +2. `faas-cli up -f go-openfaas.yml` + + Then you can invoke your go function by OpenFaas UI or faas-cli. diff --git a/FaaS/OpenFaaS/template/go-clearlinux/bundles.txt b/FaaS/OpenFaaS/template/go-clearlinux/function/bundles.txt similarity index 100% rename from FaaS/OpenFaaS/template/go-clearlinux/bundles.txt rename to FaaS/OpenFaaS/template/go-clearlinux/function/bundles.txt diff --git a/FaaS/OpenFaaS/template/node-clearlinux/Dockerfile b/FaaS/OpenFaaS/template/node-clearlinux/Dockerfile new file mode 100644 index 0000000..00571b0 --- /dev/null +++ b/FaaS/OpenFaaS/template/node-clearlinux/Dockerfile @@ -0,0 +1,62 @@ +FROM openfaas/of-watchdog:0.7.2 as watchdog + +FROM clearlinux/node:12 as ship + +COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog +RUN chmod +x /usr/bin/fwatchdog + +RUN useradd app + +WORKDIR /root/ + +# install bundles if required +# PLEASE input bundle line by line +COPY function/bundles.txt . +RUN for bundle in $(cat bundles.txt); do \ + swupd bundle-add $bundle $swupd_args; \ + done + +# Turn down the verbosity to default level. +ENV NPM_CONFIG_LOGLEVEL warn + +# Wrapper/boot-strapper +WORKDIR /home/app +COPY package.json ./ + +# This ordering means the npm installation is cached for the outer function handler. +RUN npm i + +# Copy outer function handler +COPY index.js ./ + +# COPY function node packages and install, adding this as a separate +# entry allows caching of npm install +WORKDIR /home/app/function +COPY function/*.json ./ +RUN npm i || : + +# COPY function files and folders +COPY function/ ./ + +# Set correct permissions to use non root user +WORKDIR /home/app/ + +# chmod for tmp is for a buildkit issue (@alexellis) +RUN chown app:app -R /home/app \ + && chmod 777 /tmp + +USER app + +ENV cgi_headers="true" +ENV fprocess="node index.js" +ENV mode="http" +ENV upstream_url="http://127.0.0.1:3000" + +ENV exec_timeout="10s" +ENV write_timeout="15s" +ENV read_timeout="15s" + +HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1 + +CMD ["fwatchdog"] + diff --git a/FaaS/OpenFaaS/template/node-clearlinux/README.md b/FaaS/OpenFaaS/template/node-clearlinux/README.md new file mode 100644 index 0000000..c73bb10 --- /dev/null +++ b/FaaS/OpenFaaS/template/node-clearlinux/README.md @@ -0,0 +1,28 @@ +Based on clearlinux/node:12 container image. + +### node-clearlinux +1. `faas-cli new --lang node-clearlinux node-openfaas --prefix=""` + + Files tree as below. +> + ├── node-openfaas + │   ├── bundles.txt + │   ├── handler.js + │   └── package.json + ├── node-openfaas.yml + └── template + + + * The entry is "handler.js", do any change you want in this file. + + * Put the required Clear Linux bundles in the "bundles.txt". + For example, + + `echo "openblas" >> node-openfaas/bundles.txt` + `echo "wget" >> node-openfaas/bundles.txt` + + +2. `faas-cli up -f node-openfaas.yml` + + Then you can invoke your python function by OpenFaas UI or faas-cli. + diff --git a/FaaS/OpenFaaS/template/python3-clearlinux/bundles.txt b/FaaS/OpenFaaS/template/node-clearlinux/function/bundles.txt similarity index 100% rename from FaaS/OpenFaaS/template/python3-clearlinux/bundles.txt rename to FaaS/OpenFaaS/template/node-clearlinux/function/bundles.txt diff --git a/FaaS/OpenFaaS/template/node-clearlinux/function/handler.js b/FaaS/OpenFaaS/template/node-clearlinux/function/handler.js new file mode 100644 index 0000000..63516d5 --- /dev/null +++ b/FaaS/OpenFaaS/template/node-clearlinux/function/handler.js @@ -0,0 +1,12 @@ +"use strict" + +module.exports = async (event, context) => { + let err; + const result = { + status: "Received input: " + JSON.stringify(event.body) + }; + + return context + .status(200) + .succeed(result); +} diff --git a/FaaS/OpenFaaS/template/node-clearlinux/function/package.json b/FaaS/OpenFaaS/template/node-clearlinux/function/package.json new file mode 100644 index 0000000..3d3a3e4 --- /dev/null +++ b/FaaS/OpenFaaS/template/node-clearlinux/function/package.json @@ -0,0 +1,12 @@ +{ + "name": "function", + "version": "1.0.0", + "description": "", + "main": "handler.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/FaaS/OpenFaaS/template/node-clearlinux/index.js b/FaaS/OpenFaaS/template/node-clearlinux/index.js new file mode 100644 index 0000000..7b5241a --- /dev/null +++ b/FaaS/OpenFaaS/template/node-clearlinux/index.js @@ -0,0 +1,113 @@ +// Copyright (c) Alex Ellis 2017. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +"use strict" + +const express = require('express') +const app = express() +const handler = require('./function/handler'); +const bodyParser = require('body-parser') + +// app.use(bodyParser.urlencoded({ extended: false })); +app.use(bodyParser.json()); +app.use(bodyParser.raw()); +app.use(bodyParser.text({ type : "text/*" })); +app.disable('x-powered-by'); + +class FunctionEvent { + constructor(req) { + this.body = req.body; + this.headers = req.headers; + this.method = req.method; + this.query = req.query; + this.path = req.path; + } +} + +class FunctionContext { + constructor(cb) { + this.value = 200; + this.cb = cb; + this.headerValues = {}; + this.cbCalled = 0; + } + + status(value) { + if(!value) { + return this.value; + } + + this.value = value; + return this; + } + + headers(value) { + if(!value) { + return this.headerValues; + } + + this.headerValues = value; + return this; + } + + succeed(value) { + let err; + this.cbCalled++; + this.cb(err, value); + } + + fail(value) { + let message; + this.cbCalled++; + this.cb(value, message); + } +} + +var middleware = async (req, res) => { + let cb = (err, functionResult) => { + if (err) { + console.error(err); + + return res.status(500).send(err.toString ? err.toString() : err); + } + + if(isArray(functionResult) || isObject(functionResult)) { + res.set(fnContext.headers()).status(fnContext.status()).send(JSON.stringify(functionResult)); + } else { + res.set(fnContext.headers()).status(fnContext.status()).send(functionResult); + } + }; + + let fnEvent = new FunctionEvent(req); + let fnContext = new FunctionContext(cb); + + Promise.resolve(handler(fnEvent, fnContext, cb)) + .then(res => { + if(!fnContext.cbCalled) { + fnContext.succeed(res); + } + }) + .catch(e => { + cb(e); + }); +}; + +app.post('/*', middleware); +app.get('/*', middleware); +app.patch('/*', middleware); +app.put('/*', middleware); +app.delete('/*', middleware); + +const port = process.env.http_port || 3000; + +app.listen(port, () => { + console.log(`OpenFaaS Node.js listening on port: ${port}`) +}); + +let isArray = (a) => { + return (!!a) && (a.constructor === Array); +}; + +let isObject = (a) => { + return (!!a) && (a.constructor === Object); +}; diff --git a/FaaS/OpenFaaS/template/node-clearlinux/package.json b/FaaS/OpenFaaS/template/node-clearlinux/package.json new file mode 100644 index 0000000..51b667c --- /dev/null +++ b/FaaS/OpenFaaS/template/node-clearlinux/package.json @@ -0,0 +1,16 @@ +{ + "name": "node10-express", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "body-parser": "^1.18.2", + "express": "^4.16.2" + } +} diff --git a/FaaS/OpenFaaS/template/node-clearlinux/template.yml b/FaaS/OpenFaaS/template/node-clearlinux/template.yml new file mode 100644 index 0000000..7ab445b --- /dev/null +++ b/FaaS/OpenFaaS/template/node-clearlinux/template.yml @@ -0,0 +1,7 @@ +language: node-clearlinux +fprocess: node index.js +welcome_message: | + You have created a new function which uses Node.js 12 and the OpenFaaS + of-watchdog. + npm i --save can be used to add third-party packages like request or cheerio + npm documentation: https://docs.npmjs.com/ diff --git a/FaaS/OpenFaaS/template/numpy-mp/Dockerfile b/FaaS/OpenFaaS/template/numpy-mp/Dockerfile new file mode 100644 index 0000000..0f14cb4 --- /dev/null +++ b/FaaS/OpenFaaS/template/numpy-mp/Dockerfile @@ -0,0 +1,39 @@ +FROM openfaas/classic-watchdog:0.18.1 as watchdog + +FROM clearlinux/python:3 + +ARG swupd_args +RUN swupd bundle-add python-extras $swupd_args + +COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog +RUN chmod +x /usr/bin/fwatchdog + +RUN useradd numpy-func +WORKDIR /home/numpy-func + +COPY function /home/numpy-func/function +WORKDIR /home/numpy-func/function +# pip install python packages if required +RUN pip install -r requirements.txt + +# install bundles if required +# PLEASE input bundle line by line +RUN for bundle in $(cat bundles.txt); do \ + swupd bundle-add $bundle $swupd_args; \ + done + +# give a chance to run help script +RUN chmod +x helper_script.sh && ./helper_script.sh +RUN chmod +x numpy-entry.sh && chmod +x set-num-threads.sh \ + && chown numpy-func -R /home/numpy-func \ + && mkdir /usr/local/bin \ + && mv numpy-entry.sh /usr/local/bin \ + && mv set-num-threads.sh /usr/local/bin + +USER numpy-func +ENV fprocess="numpy-entry.sh" +EXPOSE 8080 + +HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1 + +CMD ["fwatchdog"] diff --git a/FaaS/OpenFaaS/template/numpy-mp/README.md b/FaaS/OpenFaaS/template/numpy-mp/README.md new file mode 100644 index 0000000..5a89cde --- /dev/null +++ b/FaaS/OpenFaaS/template/numpy-mp/README.md @@ -0,0 +1,40 @@ +Based on clearlinux/python:3 container image. +Current version is 3.8. +It can set OMP_NUM_THREADS dynamically at runtime to accelerate +performance according to assigned computing resources. + +### numpy-clearlinux +1. `faas-cli new --lang numpy-mp numpy-openfaas --prefix=""` + + Files tree as below. +> + ├── numpy-openfaas + │   ├── bundles.txt + │   ├── handler.py + │   ├── helper_script.sh + │   ├── main.py + │   ├── numpy-entry.sh + │   ├── requirements.txt + │   └── set-num-threads.sh + ├── numpy-openfaas.yml + └── template + + * Put the required python packages in the "requirements.txt". + For example, + + `echo "redis" >> numpy-openfaas/requirements.txt` + `echo "flask" >> numpy-openfaas/requirements.txt` + + * Put the required Clear Linux bundles in the "bundles.txt". + For example, + + `echo "openblas" >> numpy-openfaas/bundles.txt` + `echo "wget" >> numpy-openfaas/bundles.txt` + + * Put any initial/helper operation in the "help_script.sh" + + * Entry is "handler.py", do any change you want here. + +2. `faas-cli up -f numpy-openfaas.yml` + + Then you can invoke your python function by OpenFaas UI or faas-cli. diff --git a/FaaS/OpenFaaS/template/python3-clearlinux/requirements.txt b/FaaS/OpenFaaS/template/numpy-mp/function/bundles.txt similarity index 100% rename from FaaS/OpenFaaS/template/python3-clearlinux/requirements.txt rename to FaaS/OpenFaaS/template/numpy-mp/function/bundles.txt diff --git a/FaaS/OpenFaaS/template/numpy-mp/function/handler.py b/FaaS/OpenFaaS/template/numpy-mp/function/handler.py new file mode 100644 index 0000000..a7098fa --- /dev/null +++ b/FaaS/OpenFaaS/template/numpy-mp/function/handler.py @@ -0,0 +1,7 @@ +def handle(req): + """handle a request to the function + Args: + req (str): request body + """ + + return req diff --git a/FaaS/OpenFaaS/template/numpy-mp/function/helper_script.sh b/FaaS/OpenFaaS/template/numpy-mp/function/helper_script.sh new file mode 100644 index 0000000..a9bf588 --- /dev/null +++ b/FaaS/OpenFaaS/template/numpy-mp/function/helper_script.sh @@ -0,0 +1 @@ +#!/bin/bash diff --git a/FaaS/OpenFaaS/template/numpy-mp/function/main.py b/FaaS/OpenFaaS/template/numpy-mp/function/main.py new file mode 100644 index 0000000..08f3c25 --- /dev/null +++ b/FaaS/OpenFaaS/template/numpy-mp/function/main.py @@ -0,0 +1,14 @@ +import sys +import os +import handler + +def print_info(): + num = os.getenv('OMP_NUM_THREADS', 'calculated dynamically') + return "OMP_NUM_THREADS is " + num + + +if __name__ == "__main__": + st = print_info() + ret = handler.handle(st) + if ret != None: + print(ret) diff --git a/FaaS/OpenFaaS/template/numpy-mp/function/numpy-entry.sh b/FaaS/OpenFaaS/template/numpy-mp/function/numpy-entry.sh new file mode 100644 index 0000000..4e5f2a5 --- /dev/null +++ b/FaaS/OpenFaaS/template/numpy-mp/function/numpy-entry.sh @@ -0,0 +1,9 @@ +#!/bin/sh +if [ -n "$OMP_NUM_THREADS" ]; then + echo "Warning: Manual set of \"OMP_NUM_THREADS=$OMP_NUM_THREADS\" may be not better than the calculated value by script!" +else + source /usr/local/bin/set-num-threads.sh + echo "runtime calculated and exported OMP_NUM_THREADS=${OMP_NUM_THREADS}" +fi + +python3 main.py diff --git a/FaaS/OpenFaaS/template/numpy-mp/function/requirements.txt b/FaaS/OpenFaaS/template/numpy-mp/function/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/FaaS/OpenFaaS/template/numpy-mp/function/set-num-threads.sh b/FaaS/OpenFaaS/template/numpy-mp/function/set-num-threads.sh new file mode 100644 index 0000000..9456f1d --- /dev/null +++ b/FaaS/OpenFaaS/template/numpy-mp/function/set-num-threads.sh @@ -0,0 +1,57 @@ +#!/bin/sh + +# get core numbers +lcores=$(grep "^processor" -c /proc/cpuinfo) +htn=$(lscpu | grep "^Thread(s) per core" | awk '{print $NF}') +pcores=$(awk 'BEGIN{ x="'$lcores'" / "'$htn'"; printf("%.0f\n", (x == int(x))?x:int(x)+1) }') +csets=$((0x$(cat /proc/self/status | grep "^Cpus_allowed:" | awk '{print $NF}'))) +cfull=$(((1< 0 || $i > $lcores ]]; do + if [ -r /sys/devices/system/cpu/cpu$i/topology/thread_siblings_list ]; then + x=$(awk -F, '{print $1}' /sys/devices/system/cpu/cpu$i/topology/thread_siblings_list) + y=$(awk -F, '{print $2}' /sys/devices/system/cpu/cpu$i/topology/thread_siblings_list) + st=$((y-x)) + fi + ((i++)) + done + + # calculate logical & physical core number by the toplogy + lnums=0; pnums=0; mask=0; n=csets + while (( n != 0 )) + do + m=$((n & (~n + 1))) + if (( (m & mask) == 0 )); then + mask=$((mask|m|m< + ├── hello-openfaas + │   ├── bundles.txt + │   ├── handler.py + │   ├── helper_script.sh + │   ├── __init__.py + │   └── requirements.txt + ├── hello-openfaas.yml + └── template + + * Put the required python packages in the "requirements.txt". + For example, + + `echo "redis" >> hello-openfaas/requirements.txt` + `echo "flask" >> hello-openfaas/requirements.txt` + + * Put the required Clear Linux bundles in the "bundles.txt". + For example, + + `echo "openblas" >> hello-openfaas/bundles.txt` + `echo "wget" >> hello-openfaas/bundles.txt` + + * Put any initial/helper operation in the "help_script.sh" + +2. `faas-cli up -f hello-openfaas.yml` + + Then you can invoke your python function by OpenFaas UI or faas-cli.