Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r0Bjt-0042R1-PA for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Nov 2023 02:22:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r0Bjs-003TXs-22 for pgsql-hackers@arkaria.postgresql.org; Tue, 07 Nov 2023 02:22:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r0Bjr-003TVz-Oa for pgsql-hackers@lists.postgresql.org; Tue, 07 Nov 2023 02:22:47 +0000 Received: from mail-il1-x131.google.com ([2607:f8b0:4864:20::131]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r0Bjo-0056BT-QL for pgsql-hackers@lists.postgresql.org; Tue, 07 Nov 2023 02:22:47 +0000 Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-35941ccaa85so22452745ab.1 for ; Mon, 06 Nov 2023 18:22:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699323763; x=1699928563; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=GLDGy/zqpKdXvN47ptkWhS2iXaB3CyPEz2rq9B+z21E=; b=SHyyeqD5dpj3UuhUQTfe1KYFWxCAytACyBeum3rXlA6GEUZD9+0CC1JRWJlLefazV3 /s29jfD+P7uQ9cQvNycy4TF3iZOeD0C8gTW8WfPPju53jGRh+eVC8IoZVvjtApHV4jB/ dp6Xx202FNECux+TO2yT54XsUjHMjrdk5Vt+sS940iG0gtE4hRdcf5oecLjsTMwN6KfM xEukq3+EgMQmy5QrzLm9u7lUXTDENYNz2TagspBXH9ITScBx1p9gtnrQLuNMtym1XbPO NpRf4epx8stGLPN5CGVLfFAENdmW6uMxFd+txKzeBKHF2wbRcQ6V3kIpogQlJ7bGxzPd 88dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699323763; x=1699928563; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=GLDGy/zqpKdXvN47ptkWhS2iXaB3CyPEz2rq9B+z21E=; b=XJ/lFRSvxHmHghbmbH3/mWezvP9K616KOlzf0l/YeWbOVQdQdsnfumdqlZPWLMXwI0 GG/2umODgBI7E8PSqRZ5DDmfOgG+x0DdjMmxIO7ptAgELuzanStkRUSNqJzIM7i1S7aR 4yV2AZscUOZLSpev8LX7/UQaz6nQmdt04DBoTgzON/alC0eDjWgmMWm4/MNxYQsKjJ3E XdYYrXqpFSB/cuq85rxG1UcKzwJATAC2gQeVJSACdq/btdAfLzuyeDhSNGokmstWg4Vp nj/9zPlyBwJO65p5+uP0rhgCJv7nHgpkgfY9yaZiT3lbpRQN71Bqf2SmWs9AcSSXXWg4 MqOQ== X-Gm-Message-State: AOJu0YxrnmnyZd2LAHn98Pevo+/poTGmJSplUBwPl9HeCPB9znXjFtmu NH3WSIme3zUQo/0e/h6+HrA= X-Google-Smtp-Source: AGHT+IEkSVQH23H8MBnoCZKNHKeeuabTxxfILMv1ie2qdm+xF01hGavYAs8YtjIUTnUf7vvA0rcCDw== X-Received: by 2002:a05:6e02:2163:b0:357:a01f:35f0 with SMTP id s3-20020a056e02216300b00357a01f35f0mr1597078ilv.19.1699323762814; Mon, 06 Nov 2023 18:22:42 -0800 (PST) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id r15-20020a92d98f000000b0035134f1a240sm2810133iln.86.2023.11.06.18.22.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 18:22:42 -0800 (PST) Date: Mon, 6 Nov 2023 20:22:40 -0600 From: Nathan Bossart To: Matthias van de Meent Cc: "Amonson, Paul D" , "pgsql-hackers@lists.postgresql.org" , "Shankaran, Akash" Subject: Re: Popcount optimization using AVX512 Message-ID: <20231107022240.GA729644@nathanxps13> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Nov 03, 2023 at 12:16:05PM +0100, Matthias van de Meent wrote: > On Thu, 2 Nov 2023 at 15:22, Amonson, Paul D wrote: >> This proposal showcases the speed-up provided to popcount feature when >> using AVX512 registers. The intent is to share the preliminary results >> with the community and get feedback for adding avx512 support for >> popcount. >> >> Revisiting the previous discussion/improvements around this feature, I >> have created a micro-benchmark based on the pg_popcount() in >> PostgreSQL's current implementations for x86_64 using the newer AVX512 >> intrinsics. Playing with this implementation has improved performance up >> to 46% on Intel's Sapphire Rapids platform on AWS. Such gains will >> benefit scenarios relying on popcount. Nice. I've been testing out AVX2 support in src/include/port/simd.h, and the results look promising there, too. I intend to start a new thread for that (hopefully soon), but one open question I don't have a great answer for yet is how to detect support for newer intrinsics. So far, we've been able to use function pointers (e.g., popcount, crc32c) or deduce support via common predefined compiler macros (e.g., we assume SSE2 is supported if the compiler is targeting 64-bit x86). But the former introduces a performance penalty, and we probably want to inline most of this stuff, anyway. And the latter limits us to stuff that has been around for a decade or two. Like I said, I don't have any proposals yet, but assuming we do want to support newer intrinsics, either open-coded or via auto-vectorization, I suspect we'll need to gather consensus for a new policy/strategy. > Apart from the two type functions bytea_bit_count and bit_bit_count > (which are not accessed in postgres' own systems, but which could want > to cover bytestreams of >BLCKSZ) the only popcount usages I could find > were on objects that fit on a page, i.e. <8KiB in size. How does > performance compare for bitstreams of such sizes, especially after any > CPU clock implications are taken into account? Yeah, the previous optimizations in this area appear to have used ANALYZE as the benchmark, presumably because of visibilitymap_count(). I briefly attempted to measure the difference with and without AVX512 support, but I haven't noticed any difference thus far. One complication for visiblitymap_count() is that the data passed to pg_popcount64() is masked, which requires a couple more intructions when you're using the intrinsics. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com